Search code examples
androidandroid-cursoradapter

CursorAdapter - List beheivour


I use a CursorAdapter with one single customized layout for my list, depending on the value of a field (true or false) of my table, the color of one of the textviews in the list item will be different.

public class CustomAdapter extends CursorAdapter {
    DatabaseHelper myDB;
    SQLiteDatabase db;

public CustomAdapter(Context context, Cursor cursor, int flags) {
    super(context, cursor, 0);
    myDB = new DatabaseHelper(context, DatabaseHelper.DB_NAME, null, DatabaseHelper.DB_VERSION_SCHEME);
    db = myDB.getReadableDatabase();
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(R.layout.customize_cell_list, parent, false);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {

    TextView tv = (TextView) view.findViewById(R.id.tv);
    TextView tv_date = (TextView) view.findViewById(R.id.tvDateCursor);
    TextView tv_time = (TextView) view.findViewById(R.id.tvTime);
    ImageView img = (ImageView)view.findViewById(R.id.img);

    if(cursor != null) {
        String tv = cursor.getString(2);
        String date = cursor.getString(3);
        String time = cursor.getString(8);
        boolean active = cursor.getInt(12) > 0;
        boolean go = cursor.getInt(13) > 0;

        tv_date.setText(date);
        tv.setText(tv);

        if(go) { // OPTION 1
            img.setImageResource(R.drawable.calle);
            tv_time.setText(time);

        } else { // OPTION 2
            img.setImageResource(R.drawable.casa);

            if(active){  // OPTION 2.1
                tv_time.setText("Activado");
            }else{  // OPTION 2.2
                tv_time.setText("No activado");
                tv_time.setTextColor(Color.RED);
            }
        }
    } 
}
}

The result: images display the correct one the same than the textviews, the problem is the color, when I have one item with the OPTION 2.2 and start scrolling up and down some of my tv_time (even from the option 1) begin to change the color to Red, then I scroll again and they become white and others change to red, it is something random.

Why is this happening? How can I keep the red color only when "go" and "active" are false?

Thanks


Solution

  • You need to remember the following things 1. The views in the ListView are limited. 2. Whenever you scroll the View whose visibility is gone will be updated with the later items and then it will come back to the view

    So now you are setting the color based on some criteria. and whenever you are binding the view u are setting the color. Make sure when unbinding the view you will make the view go back to the original color. if u don't do this the View when looses its visibility it still contains the color and when other data item is fetched into it that looks in red color.

    So when binding first check whether it is colored red. If it is then check do you want to keep it red, and yes keep it and no then make it go back to original color