问题
I can show the id, title and borrowed values in a TextView
. My problem is that I would like to check the "borrowed" values, and if they are not null
, display a simple image. If the value is null
, I don't want to display anything.
Might work with a ViewBinder
, but I don't know how to solve this.
private static String[] FROM = { _ID, TITLE, BORROWED };
private static String[] TO = { R.id.id, R.id.title, R.id.borrowed };
private Cursor getMovies() {
SQLiteDatabase db = movies.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, FROM, null, null, null, null, ORDER_BY);
startManagingCursor(cursor);
return cursor;
}
private void showTitles(Cursor cursor) {
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor, FROM, TO);
setListAdapter(adapter);
}
回答1:
Extend your SimpleCursorAdapter and check for null of the BORROWED val in your overrided bindView() method. Something like this:
private class mySimpleCursorAdapter extends SimpleCursorAdapter{
@Override
public void bindView(View view, Context context, Cursor cursor) {
if ( cursor.isNull( cursor.getColumnIndex(BORROWED) ) ){
// Handle NULL scenario
} else {
// Handle not NULL scenario
}
}
}
This is not tested and there's other code you'll need to implement, but this is the idea.
回答2:
You can apply where clause in db.query for non null values so that your cursor only has not null rows..
来源:https://stackoverflow.com/questions/7272899/skip-null-values-in-listactivity-with-sqlite-data