我有7个textview代表日期ex。 8月1日,8月2日,8月3日...这7个textview出现在listview的每一行,问题是当我点击按钮时,所有textview都显示在listview的最后一项中。
这就是我想要实现的目标 http://img818.imageshack.us/img818/416/q39d.png
listitem0
8月1日
listitem1
8月2日
...
这是我在getview中的代码
public View getView(int position, View convertView, ViewGroup parent) {
//august1
Calendar mCalendar = new GregorianCalendar();
mCalendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
SimpleDateFormat mDF = new SimpleDateFormat("dd MMMM");
String fDate = mDF.format(mCalendar.getTime());
//august2
mCalendar.add(Calendar.DAY_OF_MONTH, 1);
String secDate = mDF.format(mCalendar.getTime());
mCalendar.add(Calendar.DAY_OF_MONTH, 1);
String tDate = mDF.format(mCalendar.getTime());
mCalendar.add(Calendar.DAY_OF_MONTH, 1);
String fourthDate = mDF.format(mCalendar.getTime());
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.activity_main, parent, false);
fDay = (TextView) rowView.findViewById(R.id.fDate);
secDay = (TextView) rowView.findViewById(R.id.secDate);
tDay = (TextView) rowView.findViewById(R.id.tDate);
fourthDay = (TextView) rowView.findViewById(R.id.fourthDate);
Button test = (Button) rowView.findViewById(R.id.btn);
test.setOnClickListener(new OnClickListener() {
//to increment the date
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
weekNumber = weekNumber + 1;
fDay.setText(getNextWeek(weekNumber));
}
});
fDay.setText(values[position]);
//text.setText(values[position]);
// Change icon based on name
String s = values[position];
//String bs = values[position];
System.out.println(s);
//System.out.println(bs);
//represent date in a row
//listitem0
if (s.equals("Date 1")) {
fDay.setText(fDate);
//listitem1
} else if (s.equals("Date 2")) {
secDay.setText(secDate);
//listitem2
} else if (s.equals("Date 3")) {
tDay.setText(tDate);
} else if (s.equals("Date 4")) {
fourthDay.setText(fourthDate);
......
} else {
// textView.setText(fDate);
}
return rowView;
}
// method to adjust the date
private static String getNextWeek(int weekFromToday) {
Calendar mCalendar = new GregorianCalendar();
mCalendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
mCalendar.set(Calendar.WEEK_OF_YEAR,
mCalendar.get(Calendar.WEEK_OF_YEAR) + weekFromToday);
SimpleDateFormat mDF = new SimpleDateFormat("dd MMMM");
String fDate = mDF.format(mCalendar.getTime());
fDay.setText(fDate);
mCalendar.add(Calendar.DAY_OF_MONTH, 1);
String secDate = mDF.format(mCalendar.getTime());
mCalendar.add(Calendar.DAY_OF_MONTH, 1);
String tDate = mDF.format(mCalendar.getTime());
mCalendar.add(Calendar.DAY_OF_MONTH, 1);
String fourthDate = mDF.format(mCalendar.getTime());
fDay.setText(fDate);
secDay.setText(secDate);
tDay.setText(tDate);
fourthDay.setText(fourthDate);
return fDate;
}
}
答案 0 :(得分:0)
这是实现getView
方法重用列表视图的更准确方法:
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(textViewResourceId, parent, false);
} else {
view = convertView;
}
return view;
}