SimpleCursorAdapter bindView和回收的视图
问题描述:
我已经看到以前的主题像这样的主题:Android: Issue with newView and bindView in custom SimpleCursorAdapter仍然不明白我的代码是什么问题。我已经阅读过关于它的书籍等等,但是为什么我没有把它做好。SimpleCursorAdapter bindView和回收的视图
问题是,当我滚动我的listView我得到错误的数据行,但如果我点击行并进入下一个活动它对应于它应该有的数据。
我实现了viewHolder来绑定日期,以绑定到newView/bindview方法中的行。
一切都很好,直到我开始滚动列表视图。这就是行开始混合起来的时候。这与回收意见有关,我知道,如何解决这个问题,我仍然试图弄清楚。我会爱任何帮助!
我SimplecursorAdapter的代码:
public class DropNotesAdapter extends SimpleCursorAdapter {
private LayoutInflater layoutInflater;
private Utils mUtils = new Utils();
private int layout;
private int titleColIndex;
private int modifiedColIndex;
private int priorityColIndex;
public DropNotesAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.layout = layout;
layoutInflater = LayoutInflater.from(context);
titleColIndex = c.getColumnIndex(DropNotesDBAdapter.KEY_TITLE);
modifiedColIndex = c.getColumnIndex(DropNotesDBAdapter.KEY_MODIFIED);
priorityColIndex =c.getColumnIndex(DropNotesDBAdapter.KEY_PRIORITY);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = layoutInflater.inflate(layout, parent, false);
TextView titleText = (TextView) view.findViewById(R.id.title_line);
TextView modifiedText = (TextView) view.findViewById(R.id.date_line);
ImageView priorityTag = (ImageView) view.findViewById(R.id.item_priority);
NoteHolder holder = new NoteHolder();
holder.titleView = titleText;
holder.modifiedView = modifiedText;
holder.priorityView = priorityTag;
holder.title = cursor.getString(titleColIndex);
holder.modified = mUtils.formatDate(mUtils.formatDateFromString (cursor.getString(modifiedColIndex), context, "dd-MM-yyyy"));
holder.priorityResId = mUtils.getPriorityResourceId(cursor.getInt(priorityColIndex));
view.setTag(holder);
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
NoteHolder holder = (NoteHolder) view.getTag();
holder.titleView.setText(holder.title);
holder.modifiedView.setText(holder.modified);
holder.priorityView.setImageResource(holder.priorityResId);
Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/ARLRDBD.TTF");
holder.titleView.setTypeface(tf);
holder.modifiedView.setTypeface(tf);
}
private static class NoteHolder {
TextView titleView;
TextView modifiedView;
ImageView priorityView;
String title;
String modified;
int priorityResId;
}
}
答
得到NoteHolder
摆脱title
,modified
和priorityResId
。持有者拥有相关行中的小部件。持有者不是持有模型数据。您可以从Cursor
获取bindView()
中的模型数据。
这是如此简单..我看到了保存列的例子,并认为为什么不保存数据本身...非常感谢!为我节省了很多时间和头痛! – greven 2011-12-31 22:04:24
标签持有人模式怎么样,我仍然得到重复! – Skynet 2013-12-19 09:04:27