SimpleCursorAdapter bindView和回收视图

时间:2011-12-31 21:47:08

标签: android simplecursoradapter

我之前看过有关此主题的主题,例如:Android: Issue with newView and bindView in custom SimpleCursorAdapter,但仍无法理解代码的问题。我已经阅读了它上面的书等等,并且无法理解为什么我没有把它弄好。

问题是当我滚动我的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;
}

}

1 个答案:

答案 0 :(得分:2)

title中删除modifiedpriorityResIdNoteHolder。持有者持有相关行中的小部件。持有者持有模型数据。您可以从bindView()获取Cursor中的模型数据。