我有一个 ListView 和一个 SimpleCursorAdapter。我添加了两行(每行都是一个提醒):第一行有标题、日期和倒计时字符串,第二行有标题和地址(位置)。出于某种原因,来自第 2 行的 ViewHolder 的 mCountDownString 具有来自第 1 行的值。它不是为每一行创建一个 ViewHolder 对象吗?如果是,为什么地址提醒行的 mCountDownString 不为空?
private static class ViewHolder {
TextView mTitle;
TextView mDate;
String mDateString;
TextView mCountDown = null;
String mCountDownString;
TextView mAddress;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
mCursor = (Cursor) getItem(position);
if(convertView == null)
{
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.row, parent, false);
holder.mTitle = (TextView) convertView.findViewById(R.id.titleID);
//holder.mAddress = (TextView) convertView.findViewById(R.id.dateTimeOrLocationID);
//holder.mDate = (TextView) convertView.findViewById(R.id.dateTimeOrLocationID);
// edit: changed to use a separate view
holder.mAddress = (TextView) convertView.findViewById(R.id.addressId);
holder.mDate = (TextView) convertView.findViewById(R.id.dateTimeId);
holder.mCountDown = (TextView) convertView.findViewById(R.id.countdownID);
holder.mCountDownString = null;
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
int colorPos = mCursor.getPosition() % colors.length;
convertView.setBackgroundColor(colors[colorPos]);
int col = mCursor.getColumnIndex(ReminderColumns.TITLE);
holder.mTitle.setText(mCursor.getString(col));
col = mCursor.getColumnIndex(ReminderColumns.ADDRESS);
String address = mCursor.getString(col);
if(address != null)
{
// No. 1: it's an address: set only title + address fields
System.out.println("title for address: " + holder.mTitle.getText());
holder.mAddress.setText(address);
holder.mDate.setText(""); // edit
holder.mCountDown.setText("");
holder.mCountDownString = "";
}
else
{
// No. 2: it's date: set only title + date + countDownString fields
col = mCursor.getColumnIndex(ReminderColumns.DATE);
Date date = new Date(mCursor.getLong(col));
SimpleDateFormat timeFormat2 = new SimpleDateFormat("EEE, d MMM yyyy HH:mm");
String dateStr = timeFormat2.format(date);
holder.mDate.setText(dateStr);
String countDownStr = getTimeDiff(date);
holder.mCountDown.setText(countDownStr);
holder.mCountDownString = countDownStr;
holder.mAddress.setText(""); // edit
}
return convertView;
}
编辑 如果不使用,我会清除所有持有人的成员变量。但是问题仍然存在:对于日期行,它不显示 holder.mDate,对于地址行,它不显示 holder.mAddress。有些东西混在一起了。当我创建适配器时,光标最初为空。这就是我在 getView() 中设置它的原因。
编辑 2 我更改了我的代码,所以我没有为 holder.mAddress 和 holder.mDate 使用 dateTimeOrLocationID。现在的问题是,有一个占用空间的空视图。Fe 如果我添加一个日期并将 R.id.addressId 留空,则空白空间仍然可见。有没有办法避免这种情况?你介意解释一下为什么我的第一种方法不起作用吗?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:focusable="false"
android:clickable="false"
android:descendantFocusability="afterDescendants">
<TextView
android:id="@+id/titleID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textIsSelectable="false"
android:focusable="false"
android:clickable="false"
android:textColor="@color/black" />
<TextView
android:id="@+id/dateTimeId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textIsSelectable="false"
android:focusable="false"
android:clickable="false"
android:textColor="@color/black" />
<TextView
android:id="@+id/addressId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textIsSelectable="false"
android:focusable="false"
android:clickable="false"
android:textColor="@color/black" />
<TextView
android:id="@+id/countdownID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textIsSelectable="false"
android:focusable="false"
android:clickable="false"
android:textColor="@color/black" />
</LinearLayout>