Thanks to help from stackoverflow users I have managed to change colour of one specific row based on some condition. But changing colour doesn’t fully fit my needs and expectations.
So I started looking through web and trying to change it. I want to set text in TextView of specific row. To be able to change row colour I was advised to create personalised SimpleCursorAdapter class as I am getting values into ListView from SQLite using Cursor.
After reading and testing it this is what I came up with:
public class MyCursorAdapter extends SimpleCursorAdapter {
public MyCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
if(cursor.getLong(cursor.getColumnIndex(MyDBAdapter.KEY_PRICE)) == 5)
{
TextView price = (TextView)view.findViewById(R.id.priceInfo);
price.setText("High");
}
else
view.setBackgroundColor(0x00000000);
}
}
However, what I am getting now is duplicated text on some rows (there is regularity in it). As I read somewhere, every scroll is refreshing ListView, but how it is possible that when I put in if clause (insted of this TextView) this line: view.setBackgroundColor(SELECTED_COLOR); everything works fine and only this one row had been changed.
Can someone please tell me what I have to do to make it works, or what is wrong in my thinking?
Because of “itemrecycling” the view in the bindView has colors and text that is been recycled. You need to assign all properties explicitly for each bindView call that change over different listview items. in this case the backgroundcolor and price label.