views:

36

answers:

1

I've seen this question asked before but it seems that the answer may be outdated.

I'm pulling a numeric value off of a spinner and storing it in a sqlite database as a string. I have an imageview placed into the row xml file. I'm sure creating some sort of adapter is involved.

How do i change the imageview source based on the value of the string from my database?

here is what i have now. the imageview src is static and the id is located in the xml layout.

``

private void fillData() {

        String journalId = ((resource) this.getApplication()).getjournalId();

        Cursor notesCursor = mDbHelper.fetchAllPlaces(journalId);
        startManagingCursor(notesCursor);
          String[] from =  new String[]{journalDbAdapter.KEY_JOURNAL_NAME, journalDbAdapter.KEY_PLACE
                  , journalDbAdapter.KEY_DATE};
       int[] to = new int[]{R.id.placedetail1, R.id.placedetail2, R.id.placedetail3};
             SimpleCursorAdapter notes = 
            new SimpleCursorAdapter(this, R.layout.placedetailrow, notesCursor, from, to);
        setListAdapter(notes);

    }``

i have an additional field KEY_TEMP, it will have a value if a string (0 OR 1 OR 2 OR 3). i want to change the imageview src on each case.

A: 

I ended up creating my own cursor adapter: here is the part that get the value from the database and uses a switch statement for setting the image.

int healthCol = c.getColumnIndex(journalDbAdapter.KEY_HEALTH);
        int health = c.getInt(healthCol);
        ImageView health_img = (ImageView) v.findViewById(R.id.cross);
        switch(health) {
        case 0:
            health_img.setImageResource(R.drawable.cross);
            break;
        case 1:
            health_img.setImageResource(R.drawable.cross1);
        break;
        case 2:
            health_img.setImageResource(R.drawable.cross2);
            break;
        case 3:
            health_img.setImageResource(R.drawable.cross3);
            break;
        }
Brian