简体   繁体   English

尝试从 SQLite DB 设置 checkedtextview 时出错

[英]Error when trying to set checkedtextview from SQLite DB

I'm trying to get my checkedTextView state from my SQLite database but it is crashing the app.我正在尝试从我的 SQLite 数据库中获取我的 checkedTextView state,但它正在使应用程序崩溃。

Here is the code implemented:下面是实现的代码:

 Cursor c = db.rawQuery("SELECT * FROM list", null);
 int foundIndex = c.getColumnIndex("found");
 while (c != null) {
      c.getString(foundIndex);
      c.moveToNext();
 }
 String[] foundList = new String[foundIndex];
 arrayAdapter.notifyDataSetChanged();

 for (String found : levelOneListList){
      if (levelOneListList == foundList){
          levelOneListView.setItemChecked(found.indexOf(foundIndex), true);
      }
 }
}

And it is giving this error:它给出了这个错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.woodlandwanderer/com.example.woodlandwanderer.levelOneActivity}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0 java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.woodlandwanderer/com.example.woodlandwanderer.levelOneActivity}:android.database.CursorIndexOutOfBoundsException:请求索引 -1,大小为

As the error suggests, your cursor size is 0 and you are trying to access first.正如错误所暗示的,您的 cursor 大小为 0 并且您尝试先访问。

add the following check before your loop.在循环之前添加以下检查。

if (c.moveToNext() && c.getCount()>0) {

Read your cursor data like this.像这样阅读您的 cursor 数据。 Looks like your cursor size is 0.看起来您的 cursor 大小为 0。

    if (c != null && c.moveToFirst()){
        do {

           int foundIndex = c.getColumnIndex("found");
           c.getString(foundIndex);

        } while (c.moveToNext());

      c.close(); // close your db cursor
    }

// list update and set checkbox value here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM