我有一个包含5列的数据库,1列是一个TEXT,其名称为drawable,是/ res / drawable文件夹。
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中提取的数据设置背景图像?
答案 0 :(得分:3)
simpleCursorAdapter需要字符串,因此当您设置数据库时,StringArray“from”必须从列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);
因此,当您进入数据库时,您必须将TropyImage的ID(整数为整数)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[] to
和simpleCursorAdapter
似乎是正确的。您必须在ROW_TROPY
列中拥有正确的数据类型和ID。