Error when trying to set checkedtextview from SQLite DB

50 Views Asked by At

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

2

There are 2 best solutions below

5
digiwizkid On BEST ANSWER

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) {

0
Sushil Solanki On

Read your cursor data like this. Looks like your cursor size is 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