2010-07-29 62 views
0

我以前見過這個問題,但似乎答案可能已過時。基於sqlite字符串字段更改imageview src

我從一個微調器拉一個數值並將它作爲一個字符串存儲在一個sqlite數據庫中。 我有一個imageview放入行XML文件。我確定創建某種適配器。

如何根據數據庫中字符串的值更改imageview源?

這裏是我現在擁有的。 imageview src是靜態的,id位於xml佈局中。

``

私人無效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); 

    }`` 

我有一個附加的字段KEY_TEMP,它將有一個值,如果一個字符串(0或1或2或3)。 我想在每種情況下更改imageview src。

回答

0

我最終創建了自己的光標適配器: 這裏是從數據庫獲取值並使用switch語句設置圖像的部分。

 
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; 
     }