Skip to content Skip to sidebar Skip to footer

Android Simplecursoradapter - Adding Conditional Images

So I'm using SimpleCursorAdapter to adapt data from SQLite into ListView. Lets call this database testData. One of my columns in testData records true or false with either 0 or 1.

Solution 1:

I created a customized SimpleCursorAdapter:

publicclassMySimpleCursorAdapterextendsSimpleCursorAdapter {

    publicMySimpleCursorAdapter(Context context, int layout, Cursor cur,
             String[] from, int[] to) {
        super(context, layout, cur, from, to);
    }

    @OverridepublicvoidsetViewImage(ImageView iv, String text)
    {
        if (text.equals("0")) {
            iv.setImageResource(R.drawable.new1);
        }
        else {
            iv.setImageResource(R.drawable.check1);
        }
    }

}

In my ListActivity I bind the text field in the database to the image resource id. In your example it would look like this:

ListAdapter adapter = newMySimpleCursorAdapter(
    this, 
    android.R.layout.two_line_list_item,  
    mCursor,                                              
    new String[] {testData.DATE1, testData.NAME1},           
    newint[] {android.R.id.text1, android.R.id.image1}); // Note: replace text2 with image1

I.e. if the text field "Name1" in your database contains "0", the image "new1" will be displayed in the ListView, and if it has another value, "check1" will be displayed.

Post a Comment for "Android Simplecursoradapter - Adding Conditional Images"