使用图像资产文件夹将图像从 SQL 添加到 ListView
我正在制作一个包含此数据库的 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
我能做什么?我是否以正确的方式处理这个问题?
I'm making Android app that has this database in it:
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 ......`
In one of my Activities I have a search bar that can query the database. The results are displayed in a listview. When I click one item in the list I get the details for it, including a Photo.
My Photos are in the Assets Folder. I want to retrieve the photo from the assets folder and display a thumbnail of it. I tried using the Assets manager like this
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);
}
I realize this isn't correct and I'm getting the following error:
android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 7
What can I do? Am I even going about this the correct way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在进行查询后添加此行(您还应该在查询之前检查以确保光标不为null):
Add this line after you make the query(you should also check before to make sure the cursor isn't null):