基于sqlite字符串字段更改imageview src
问题描述:
我以前见过这个问题,但似乎答案可能已过时。基于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。
答
我最终创建了自己的光标适配器: 这里是从数据库获取值并使用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; }