2

I have a ListView populated from a SimpleCursorAdapter, each row containing some TextViews and an ImageView. I want to change the ImageView for each row depending on some values, however I'm trying to first figure out how to update the ImageView in the list after it's populated. I tried this code but the ImageView remains the same:

cursorAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {         
            int viewId = view.getId();         
            Log.v("ViewBinder", "columnIndex=" + columnIndex + " viewId = " + viewId);        
            if(viewId == R.id.imageloc)        
            {  
              sign2 = (ImageView) view;
              sign2.setImageResource(R.drawable.other);

              return true;          
          } 
          return false;      
          } 
        });

If anyone can help me I would appreciate it!

user
  • 86,916
  • 18
  • 197
  • 190
725623452362
  • 115
  • 2
  • 13

3 Answers3

2

If you only want to update the ImageView depending on some values, if those values are stored on the DB, you could perform the SQL query like this:

SELECT _id, name, CASE WHEN checked = 1 THEN 'R.drawable.check' ELSE 'R.drawable.check' END as img FROM table;

Doing it in this way, you would not need to use the setViewBinder() method. Just do the query, get the cursor, assign it to the SimpleCursorAdapter and assign the adapter to the list view.

Cursor c = mDbHelper.fetchData(sqlQuery);
(...)
String[] from = new String[]{"name", "img"};
int[] to = new int[]{R.id.text_view, R.id.imgageloc};
SimpleCursorAdapter _adapter = new SimpleCursorAdapter(this, R.layout.data_rows, c, from, to);
setListAdapter (_adapter);

Note: you'll need the integer value of R.drawable.check/R.drawable.check : " ... THEN '" + R.drawable.check + "' ELSE ..."

Hope it helps and is what you need.

1

Try

sign2.setBackgroundResource(R.drawable.other);

Worked for me in a very similar situation where I was trying to set an image based on an if statement.

Praveen Vinny
  • 2,372
  • 6
  • 32
  • 40
0

I can't see where you try to set different resource to ImageView. It's always R.drawable.other. Change image resource based on your cursor data.

muffinmad
  • 641
  • 3
  • 9
  • Sorry let me clarify, the imageview has a resource of R.drawable.check defined in the xml, and I want to set it to a different image if a certain criteria is met. However, I cant even get the image to change in the first place. If I can figure that out, I can take care of the rest! – 725623452362 Mar 01 '12 at 03:54
  • Oh. I guess your image view is not root view. Remove checking view ID and get ImageView this way: view.findViewById(R.id.imageloc) – muffinmad Mar 01 '12 at 05:54
  • 1
    It didnt work :( the thing is this code all works perfectly if the object is a TextView and I have it change the text....its just the moment it becomes an ImageView it doesn't want to change the image. ugh – 725623452362 Mar 01 '12 at 12:51
  • Ok. Show me please how you create adapter. – muffinmad Mar 02 '12 at 08:08
  • here it iis getListView().invalidateViews(). – user1324936 May 16 '12 at 23:16