Question

I am working on a method that queries a content provider using a cursor. After I delete a record, it calls the loadfromProvider method and refreshes the arraylist. The content provider normally has records in it however, when I delete all the records and the query runs automatically it throws exceptions. Here is my method:

private void loadFromProvider() {

    // Clear the existing array list

    EQlist.clear();

      ContentResolver cr = getContentResolver();

      // Return all the saved records
      Cursor c = cr.query(EQProvider.CONTENT_URI, null, null, null, null);

      if (c.moveToFirst()) {
        do { 

          String details = c.getString(EQProvider.DETAILS_COLUMN);
          String linkString = c.getString(EQProvider.LINK_COLUMN);

          EQli q = new EQli(details, linkString);
          addEQToArray(q);
        } while(c.moveToNext());
      }
      c.close();
      }

When I run this with no records in the content provider it throws the following:

java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)

I think this is due to the cursor trying to parse a null value. I am trying to figure out a way that if the cursor does not come back with any records, it bypasses the rest of the code and does nothing happens.

Any help would be appreciated. Thanks

Was it helpful?

Solution

If there are no records your cursor should be empty. If the cursor is empty then c.moveToFirst() will return false and you are not going to enter the do-while loop.

Debug your application and make sure that the cursor is empty.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top