In my app, I pull 3 strings from an SQlite database to populate a ListView. In the listrow I have to TextViews and an ImageView.
The two TextViews are currently populated properly(with different text in each row). But, this is not the case for the ImageView, which I set using a ViewBinder. Namely, the image in each row all get the same color. How can i fix this? Do I need to extend SimpleCursorAdapter or something?
String[] from = {DbHelper.NAME, DbHelper.COMMENT, DbHelper.COLOR};
String[] column = {DbHelper.C_ID, DbHelper.NAME, DbHelper.COMMENT, DbHelper.COLOR};
int[] to = {R.id.listitem_title, R.id.listitem_summary, R.id.listitem_color};
Cursor cursor = db.query(DbHelper.TABLE_NAME, column, null, null, null, null, null);
this.adapter = new SimpleCursorAdapter(getActivity(), R.layout.list_item_set, cursor, from, to);
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (view.getId() == R.id.listitem_color) {
cursor.moveToFirst();
String color = "#ffffff";
while (cursor.moveToNext()) {
color = cursor.getString(cursor.getColumnIndex(DbHelper.COLOR));
}
((ImageView) view).setBackgroundColor(Color.parseColor(color));
return true;
}
return false;
}
});
setListAdapter(adapter);
Namely, the image in each row all get the same color. How can i fix this?
If you're going to use:
String color = "#ffffff";
while (cursor.moveToNext()) {
color = cursor.getString(cursor.getColumnIndex(DbHelper.COLOR));
}
this will make the color
point to the color present in the last row of the Cursor
as you loop through the entire Cursor
. Instead you'll want to simply get the color
from the Cursor
:
String color = "#ffffff";
color = cursor.getString(cursor.getColumnIndex(DbHelper.COLOR));