这是 ViewHolder 模式的简单实现。我有一个包含文章的游标,我想将标题保留在行标签中。在这个阶段,我(尝试)将实际字符串和相应的 textview 保留在 row 标记中,再加上 textview 的标记中的实际字符串。
class ArticleListCursorAdapter extends SimpleCursorAdapter {
class ViewHolder {
String strTitle = null;
View viewTitle = null;
ViewHolder(View base, String strTitle) {
this.viewTitle = base.findViewById(R.id.textTitle);
this.strTitle = strTitle;
}
@Override
public String toString() {
return new StringBuffer()
.append("strTtitle [").append(strTitle).append("] ")
.append("viewTitle [").append(((TextView)viewTitle).getText()).append("] ")
.append("viewTitle title (tag) [").append(viewTitle.getTag()).append("] ")
.toString();
}
}
public ArticleListCursorAdapter(Context context, int layout, Cursor c, String[] from,
int[] to, int flags) {
super(context, layout, c, from, to, flags);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = super.getView(position, convertView, parent);
ViewHolder holder = null;
// if (convertView != null)
holder = (ViewHolder)row.getTag();
Log.v(TAG, "getView for [" + position + "]. holder ["
+ (holder == null ? "null holder" : holder.toString()) + "]");
Log.v(TAG, "getView other info: rendering view title ["
+ ((TextView)row.findViewById(R.id.textTitle)).getText()
.toString() + "]");
if (holder == null) {
Cursor cursor = (Cursor)ArticleListCursorAdapter.this.getItem(position);
final String strTitle = cursor.getString(cursor.getColumnIndex(ArticleData.C_TITLE));
holder = new ViewHolder(row, strTitle);
Log.v(TAG, "getView setup onclick for pos [" + position + "] and title ["+ strTitle + "]");
holder.viewTitle.setTag(strTitle);
row.setTag(holder);
}
return row;
}
}
当我运行它时,我希望收到一堆:
getView for [x]. holder [strTitle [7777777] viewTitle [7777777] viewTitle title (tag) [7777777] ]
getView other info: rendering view title [7777777]
如果文章标题是 7777777。我实际上收到的是一种奇怪的混合值
(4 items visible at a time, first page)
getView for [0]. holder [null holder]
getView other info: rendering view title [1111111]
getView setup onclick for pos [0] and title [1111111]
getView for [1]. holder [null holder]
getView other info: rendering view title [2222222]
getView setup onclick for pos [1] and title [2222222]
getView for [2]. holder [null holder]
getView other info: rendering view title [3333333]
getView setup onclick for pos [2] and title [3333333]
getView for [3]. holder [null holder]
getView other info: rendering view title [4444444]
getView setup onclick for pos [3] and title [4444444]
(page down)
getView for [4]. holder [strTitle [1111111] viewTitle [5555555] viewTitle title (tag) [1111111] ]
getView other info: rendering view title [5555555]
getView for [5]. holder [null holder]
getView other info: rendering view title [6666666]
getView setup onclick for pos [5] and title [6666666]
one more down
getView for [6]. holder [strTitle [2222222] viewTitle [7777777] viewTitle title (tag) [2222222] ]
getView other info: rendering view title [7777777]
getView for [7]. holder [strTitle [3333333] viewTitle [8888888] viewTitle title (tag) [3333333] ]
getView other info: rendering view title [8888888]
getView for [8]. holder [strTitle [4444444] viewTitle [9999999] viewTitle title (tag) [4444444] ]
getView other info: rendering view title [9999999]
(scroll up)
getView for [4]. holder [strTitle [4444444] viewTitle [5555555] viewTitle title (tag) [4444444] ]
getView other info: rendering view title [5555555]
getView for [3]. holder [strTitle [3333333] viewTitle [4444444] viewTitle title (tag) [3333333] ]
getView other info: rendering view title [4444444]
(scroll up again)
getView for [2]. holder [strTitle [1111111] viewTitle [3333333] viewTitle title (tag) [1111111] ]
getView other info: rendering view title [3333333]
getView for [1]. holder [strTitle [2222222] viewTitle [2222222] viewTitle title (tag) [2222222] ]
getView other info: rendering view title [2222222]
getView for [0]. holder [strTitle [6666666] viewTitle [1111111] viewTitle title (tag) [6666666] ]
getView other info: rendering view title [1111111]
1/ 为什么 ViewHolder 的字符串部分不断变化?2 / viewTitle TextView 如何更改其标签内容(上面的 5555555 viewTitle 的标签中一次有 1111111,另一个是 444444)