我想制作自定义日历。我创建了一个由7个按钮组成的布局(linearLayout)。现在,我正在做一个ListView BaseAdapter,我想在其中为这些按钮设置文本。文本将是日期,如1月5日,但只有数字。问题是我不知道如何在getView()方法中传递我的视图之间的日期。
我尝试使用全局变量,但由于多次调用getView()方法,因此int变量显示错误结果。它没有显示5或14,而是显示17或29.如何解决这种情况?
我想要的: 1 2 3 4 5 6 7 //记住这7
8 9 10 11 12 13 14 //从那个7算起
int c; // Global varibale
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(R.layout.oneweek, null);
Button button = (Button) view.findViewById(R.id.button4);
Button button1 = (Button) view.findViewById(R.id.button12);
Button button2 = (Button) view.findViewById(R.id.button13);
Button button3 = (Button) view.findViewById(R.id.button16);
Button button4 = (Button) view.findViewById(R.id.button17);
Button button5 = (Button) view.findViewById(R.id.button18);
Button button6 = (Button) view.findViewById(R.id.button19);
Button[] buttons = new Button[]{button, button1, button2, button3, button4, button5, button6};
for(int i = 0; i<7; i++){
buttons[i].setText("" + c);
c++;
}
return view;
}
答案 0 :(得分:0)
我不知道你为什么要实现自己的日历视图。您应该使用现有的库(在我看来)
无论如何,下面这段代码应该回答你的问题
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//I suppose the layout is an viewgroup
ViewGroup view = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.oneweek, null);
Button button = (Button) view.findViewById(R.id.button4);
Button button1 = (Button) view.findViewById(R.id.button12);
Button button2 = (Button) view.findViewById(R.id.button13);
Button button3 = (Button) view.findViewById(R.id.button16);
Button button4 = (Button) view.findViewById(R.id.button17);
Button button5 = (Button) view.findViewById(R.id.button18);
Button button6 = (Button) view.findViewById(R.id.button19);
Button[] buttons = new Button[]{button, button1, button2, button3, button4, button5, button6};
for(int i = 0; i<7; i++){
buttons[i].setText((i+1) + (7 * position));
//You have to add the buttons to the parent
view.addView(buttons[i])
}
return view;
}
如果您决定按照这种方式行事。您还应该实现ViewHolder模式