greven
greven

Reputation: 643

SimpleCursorAdapter bindView and recycled views

I've seen previous topics on the subject like this one: Android: Issue with newView and bindView in custom SimpleCursorAdapter and still can't understand what the problem with my code is. I've read books on it, etc, etc and can't get it why I'm not getting it right.

The problem is when I scroll my listView I get rows with the wrong data but if I click the row and get into the next activity it correspond to the data it's supposed to have.

I implemented a viewHolder for the date to bind to the rows in the newView / bindview methods.

Everything displays well till I start to scroll the list view. That's when the rows start to get all mixed up. That has to do with the recycling of views, that I know, how to fix it, that I'm still trying to figure it out. I would love any help!

The code of my 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;
}

}

Upvotes: 1

Views: 4049

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006819

Get rid of title, modified, and priorityResId from NoteHolder. A holder holds widgets that are in the associated row. A holder does not hold model data. You get your model data in bindView() from the Cursor.

Upvotes: 2

Related Questions