Skip null values in ListActivity with SQLite data

和自甴很熟 提交于 2020-01-07 01:28:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!