我想在我的SQlite数据库中存储一个drawable(如R.drawable.image)的ID,然后检索该ID以放入listView。我不想将图像保存为BLOB我只想存储ID。
现在我创建一个这样的表,我存储了一个drawable的ID:
db.execSQL("CREATE TABLE cards \n" +
"(\n" +
"card_id INT PRIMARY KEY, \n" +
"cpr VARCHAR(11) NOT NULL,\n" +
"card_type VARCHAR(30), \n" +
"front_photo VARCHAR(30), back_photo BLOB,\n" +
"FOREIGN KEY (cpr) REFERENCES users(cpr)\n" +
")\n" +
";\n");
db.execSQL("INSERT INTO cards (card_id , cpr, card_type, front_photo) VALUES(1, '170492-1802','Rejsekort', 'R.drawable.rejsekort_f');"
);
db.execSQL("INSERT INTO cards (card_id , cpr, card_type, front_photo) VALUES(2, '170492-1802','Bank Card', 'R.drawable.bankcard_f');"
);
db.execSQL("INSERT INTO cards (card_id , cpr, card_type, front_photo) VALUES(3, '170492-1802','Drivers Licence', 'R.drawable.bankcard_f');"
);
db.execSQL("INSERT INTO cards (card_id , cpr, card_type, front_photo) VALUES(4, '170492-1802','Sundhedskort', 'R.drawable.bankcard_f');"
);
db.execSQL("INSERT INTO cards (card_id , cpr, card_type, front_photo) VALUES(5, '170492-1802','Ungdomskort', 'R.drawable.bankcard_f');"
);
然后我尝试获取该表的所有ID并将它们添加到Integer ArrayList中,以便我可以在ListView中显示它们:
ArrayList<Integer> images=new ArrayList<Integer>();
public void addImagesToListview() {
DBHelper dbHelper = new DBHelper(MainActivity.this);
db = dbHelper.getReadableDatabase();
Cursor getImages = db.rawQuery("Select front_photo from cards", null);
getImages.moveToFirst();
while(!getImages.isAfterLast()) {
String image = getImages.getString(0);
int image2 = Integer.parseInt(image);
images.add(image2);
getImages.moveToNext(); }
但由于某种原因,应用程序崩溃了?我可以在我的ArrayList中添加ID的drawables,如下所示:
images.add(R.drawable.image);
那么为什么当我将该ID存储在数据库中并再次检索它时,它是否有效,所以它看起来像这样:
images.add("ID from database");
答案 0 :(得分:0)
将您的数据库更改为
front_photo INT
当您插入行时,插入评估的ID,而不是表示它的字符串。
int photoID = R.drawable.rejsekort_f;
db.execSQL("INSERT INTO cards (card_id , cpr, card_type, front_photo) VALUES(1, '170492-1802','Rejsekort', " + photoID + ");";