私は ListView と SimpleCursorAdapter を持っています。2 つの行を追加します (それぞれがリマインダーです)。最初の行にはタイトル、日付、カウントダウン文字列があり、2 番目の行にはタイトルと住所 (場所) があります。何らかの理由で、ViewHolder の行 #2 の 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;
}
EDIT 使用されていない場合は、すべての所有者のメンバー変数をクリアします。しかし、問題は解決しません。日付行の場合は holder.mDate が表示されず、住所行の場合は holder.mAddress が表示されません。何かが混じっている。アダプターを作成すると、カーソルは最初は null です。そのため、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>