javaandroidcheckedtextview

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.

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


Solution

  • As the error suggests, your cursor size is 0 and you are trying to access first.

    add the following check before your loop.

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