使用图像资产文件夹将图像从 SQL 添加到 ListView

发布于 2025-01-06 12:39:58 字数 1961 浏览 2 评论 0原文

我正在制作一个包含此数据库的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零度℉ 2025-01-13 12:39:58

在进行查询后添加此行(您还应该在查询之前检查以确保光标不为null):

cursor.moveToFirst();

Add this line after you make the query(you should also check before to make sure the cursor isn't null):

cursor.moveToFirst();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文