Skip to content Skip to sidebar Skip to footer

How Change Color/picture Of Specific One ListView Row - Android?

I am trying to change only one (maybe more) ListView row based on specific condition. I have read many answers on similar questions and tons of other tutorials but I am not able to

Solution 1:

Simply extend SimpleCursorAdapter and override bindView():

public class MyAdapter extends SimpleCursorAdapter {
    public MyAdapter(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)
            view.setBackgroundColor(0xff00ff00);
        else // this will be the default background color: transparent
            view.setBackgroundColor(0x00000000);
    }
}

Solution 2:

You can try to create your custom adapter that extends on SimpleCursorAdapter and inside that, on the method bindView, you can look if the condition you look for is accomplished and make what you want on that method.

http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html#bindView(android.view.View, android.content.Context, android.database.Cursor)

Hope to help :)


Post a Comment for "How Change Color/picture Of Specific One ListView Row - Android?"