Enhancing the SimpleCursorAdapter to check the data in Android?
I have a sqlite database. I am able to query it and get a cursor with results. It populates the data well in a list view which has 1 image view and 2 text views. However the cursor result set which has 2 columns is of text. This is shown in the 2 text views. Everything till here works fine.
Now based on the value of one of the column I need to show di开发者_StackOverflow中文版fferent images in the image view in the list item. How do I do this. As of now I am using the below code. How do I modify it to get a image view changes based on the column value?
Cursor mCursor = mDbHelper.fetchAll(); startManagingCursor(mCursor);
String[] from = new String[]{"TITLE","BODY"};
int[] to = new int[]{R.id.text1,R.id.text2};
SimpleCursorAdapter listValues =
new SimpleCursorAdapter(this, R.layout.row_item, mCursor, from, to);
setListAdapter(listValues );
I got this done using code below
listValues.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int column) {
if( column == col ){
ImageView img = (ImageView) ((View)view.getParent()).findViewById(R.id.icon);
if(cursor.getString(col).equals("Y"))
{
img.setBackgroundResource(R.drawable.img1);
}
else
{
img.setBackgroundResource(R.drawable.img2);
}
return true;
}
return false;
}
});
精彩评论