我正在制作其中包含此数据库的 Android 应用程序:
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE IF NOT EXISTS snacks (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"name TEXT, " +
"disc TEXT, " +
"photo TEXT, " +
"thumb TEXT, " +
"ingre TEXT, " +
"howto TEXT, " +
"info TEXT, " +
"snackId INTEGER)";
db.execSQL(sql);
ContentValues values = new ContentValues();
values.put("name", "Name 1");
values.put("disc", "here is the description");
values.put("photo", "stub.png");
values.put("thumb", "stub.png");
values.put("ingre", "the ingredients of the snack");
values.put("howto", "how to make this thing");
values.put("info", "basically its this much calorie and such and such");
db.insert("snacks", "name", values);...............etc etc ......`
在我的一项活动中,我有一个可以查询数据库的搜索栏。结果显示在列表视图中。当我单击列表中的一个项目时,我会得到它的详细信息,包括一张照片。我的照片在资产文件夹中。我想从资产文件夹中检索照片并显示它的缩略图。我尝试像这样使用资产管理器
public void search(View view) {
cursor = db.rawQuery("SELECT _id, name, disc, thumb FROM snacks WHERE name LIKE ?", new String[]{"%" + searchText.getText().toString() + "%"});
adapter = new SimpleCursorAdapter(
this,
R.layout.cook_tab_snackslist,
cursor,
new String[] {"name", "disc"},
new int[] {R.id.name, R.id.disc});
Thumb = (ImageView) findViewById(R.id.thumb);
try {
InputStream bitmap=getAssets().open(cursor.getString(cursor.getColumnIndexOrThrow("thumb")));
Bitmap bit=BitmapFactory.decodeStream(bitmap);
Thumb.setImageBitmap(bit);
} catch (IOException e1) {
e1.printStackTrace();
}
setListAdapter(adapter);
}
我意识到这是不正确的,我收到以下错误:
android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 7
我能做些什么?我什至会以正确的方式解决这个问题吗?