Android Custom SimpleCursorAdapter to set image in list column


Use the following custom SimpleCursorAdapter in Android and set image view in the listview column. 

mAdapter = new MySimpleCursorAdapter(getActivity(), R.layout.list_layout_example, c,
new String[] { RecentCallsTable.COLUMN_CALLER_NAME, RecentCallsTable.COLUMN_CALLER_LOCATION },
new int[] { R.id.text1, R.id.text2 });
              setListAdapter(mAdapter);

private class MySimpleCursorAdapter extends SimpleCursorAdapter {

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

   public void bindView(View v, Context context, Cursor c) {
   
      String name = c.getString(c.getColumnIndex(RecentCallsTable.COLUMN_CALLER_NAME));
          String location = c.getString(c.getColumnIndex(RecentCallsTable.COLUMN_CALLER_LOCATION));


          TextView name_text = (TextView) v.findViewById(R.id.text1);
          if (name_text != null) {
              name_text.setText(name);
          }

         ImageView im = (ImageView) v.findViewById(R.id.text2);          
         im.setBackgroundResource(R.drawable.ic_call_unmute);
   }

}

2 comments: