views:

2468

answers:

1

I have a xml file name "list_row.xml", this was load in a listView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
 android:id="@+id/icon"
 android:layout_width="wrap_content"  
 android:layout_height="wrap_content"
 android:src="@drawable/icon_one"
/>
<TextView
 android:id="@+id/label"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"  
/>

I use a function to load data from my table and fill in my "list_row.xml" file

private void fillData() {       
 Cursor c = mDbHelper.fetchAllNotes();
 String[] from = new String[]{ ListDbAdapter.KEY_ROWID,
   ListDbAdapter.KEY_ICON, ListDbAdapter.KEY_LABEL };
 int[] to = new int[]{ R.id.id, R.id.icon, R.id.label };
 SimpleCursorAdapter adapter = 
  new SimpleCursorAdapter(this, R.layout.list_row, c, from, to );  
 setListAdapter(adapter);
}

My question is how can I check the icon value to set the icon display on my listview (my data have to icon: icon_one, icon_two). Can someone help me with the code?

+3  A: 

From the documentation:

Binding occurs in two phases. First, if a SimpleCursorAdapter.ViewBinder is available, setViewValue(android.view.View, android.database.Cursor, int) is invoked. If the returned value is true, binding has occured. If the returned value is false and the view to bind is a TextView, setViewText(TextView, String) is invoked. If the returned value is false and the view to bind is an ImageView, setViewImage(ImageView, String) is invoked.

So, override setViewImage() and manually associate your icon with the ImageView. Or, override newView() and bindView() and manually bind your whole rows.

CommonsWare