【问题标题】:Adding Images to a ListView from SQL using Assets Folder for Images使用图像的资产文件夹从 SQL 将图像添加到 ListView
【发布时间】:2012-02-16 19:20:08
【问题描述】:

我正在制作包含此数据库的 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

我能做什么?我什至会以正确的方式解决这个问题吗?

【问题讨论】:

  • slukian 的回答是正确的。只是为了扩展它 - 查询返回的光标位置设置为“在”第一条记录之前。由于游标的第一条记录的“索引”为 0,因此游标位置为 -1 是有意义的。正如 slukian 所说,您需要通过调用 moveToFirst 或简单地调用 moveToNext 来移动光标位置。
  • 现在,当我搜索不存在的东西时,应用程序崩溃并出现以下错误:.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0... 有什么想法吗?

标签: android listview android-listview imageview android-assets


【解决方案1】:

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

cursor.moveToFirst();

【讨论】:

  • 谢谢,它停止了我的错误,但仍然没有任何反应,我似乎无法在 listView 中显示图像...
  • 现在,如果我查询一个不在我列表中的字母,应用程序崩溃错误报告,.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
  • 好的,我在阅读后修复了新错误: if (cursor.getCount() == 1) { cursor.moveToFirst();但仍然没有显示缩略图:(帮助!
  • @FirstLaw 解决CursorIndexOutOfBounds 的更简单方法是使用cursor.moveToFirst() 方法,如果光标为空,它将返回false。要使您的图像出现,您必须实现自己的 SimpleCursorAdapter 并在方法 bindView() 中设置行的图像。
  • 你好,我很抱歉我是个菜鸟,但如果我只使用 cursor.moveTofirst() 我得到 cursur nullfied 错误,所以我必须使用这个 if (cursor.getCount() = = 1) { 首先命令使其正常,但是......然后什么都没有出现@命令?,我不知道你的意思。对不起,我是菜鸟
猜你喜欢
  • 2023-03-07
  • 2014-10-03
  • 1970-01-01
  • 1970-01-01
  • 2013-08-04
  • 2015-02-06
  • 2017-07-16
  • 2016-12-28
  • 2021-03-30
相关资源
最近更新 更多