Search code examples
androidlistviewandroid-cursoradapter

Prevent re-use of some views in ListView (custom cursor-adapter)


Is it possible to add one view created during newView(..) as a separate type, so that reuse of just that view is prevented during bindView(...)?

This is what my custom cursorAdapter looks like:

@Override
public void bindView(View vi, Context arg1, Cursor cursor) {
    priority.setText(cursor.getString(cursor.getColumnIndex(TodoTable.COLUMN_PRIORITY)));TextView timeElapsed =  (TextView)vi.findViewById(R.id.todayTime); //time
    long id = cursor.getLong(cursor.getColumnIndex(TodoTable.COLUMN_ID));

    if(ts.getRunning() == id){
        ts.startTimer(vi, ts.getCurrentTaskStart(), id);
    }else{
        long time = cursor.getLong((cursor.getColumnIndex(TodoTable.COLUMN_TIME)));
        timeElapsed.setText(ts.getTimeString(0, 0, time));
    }
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup arg2) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View vi = inflater.inflate(R.layout.list_item, null);
    bindView(vi, context, cursor);
    return vi;
}

I tried adding it to a different type, using getItemViewType and getViewTypeCount but that can only deals with positions and not the IDs associated with the rows of the listview.

I want to prevent the view with ts.getRunning() == id at the time of creation being reused at other positions when I scroll. How would I do something like this?


Solution

  • If you want to prevent the reuse of view at a certain position the do the following

    @Override
    public int getItemViewType(int position) {
        mCursor.moveToPosition(position);
        if (mCursor.getLong(mCursor.getColumnIndex(TodoTable.COLUMN_ID)) == ts.getRunning()){
            return IGNORE_ITEM_VIEW_TYPE;
        }
        return super.getItemViewType(position);
    }
    

    If you return IGNORE_ITEM_VIEW_TYPE in getItemViewType(position) then the view at that position will not be recycled. More info about IGNORE_ITEM_VIEW_TYPE is here