2010-10-03 102 views
2

我有一个数据库有5列,1列是一个文本与可绘制的名称是/ res/drawable文件夹。Android数据库设置ImageView

private void fillData() { 

    mCursor = db2.getAllAchievements(); 
    startManagingCursor(mCursor); 


    String[] from = new String[]{achHelper.ROW_NAME, achHelper.ROW_DESCRIPTION, achHelper.ROW_POINTS, achHelper.ROW_TROPHY}; 


    int[] to = new int[]{R.id.achTitle, R.id.achDescription, R.id.achPoints, R.id.trophy}; 

    SimpleCursorAdapter classes = 
      new SimpleCursorAdapter(this, R.layout.ach_row, mCursor, from, to); 
    setListAdapter(classes); 
} 

R.id.trophy是ImageView的,我怎么可以设置基于正从achHelper.ROW_TROPHY拉数据的背景图像?

回答

3

的simpleCursorAdapter需要字符串,因此您的字符串数组“从”必须从列achHelper.ROW_TROPHY得到String对象,当你设置你的数据库,它看起来像这样:

private static final String TABLE_CREATE = "CREATE TABLE " here your other colums 
              + ROW_TROPHY + " TEXT NOT NULL);"; 
db.execSQL(TABLE_CREATE); 

所以,当你让你进入你的数据库您必须将ID转换您TropyImage的(whitch是整数)R.drawable.yourTropyImage字符串:

ContentValues cv = new ContentValues(); 
cv.put(your other columns, your other input); 
cv.put(ROW_TROPHY, Integer.toString(R.drawable.yourTrophyImage)); 

return db.insert(DATABASE_TABLE, null, cv); 

String[] from, int[] tosimpleCursorAdapter似乎是正确的。您只需在ROW_TROPY列中拥有正确的数据类型和ID。