Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android.database.CursorIndexOutOfBoundsException: Index -1 requested

Tags:

android

I'm trying to read all the contacts using RawContacts.entityIterator but I'm seeing this error:

android.database.CursorIndexOutOfBoundsException: Index -1 requested

Following is my code:

ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(Contacts.CONTENT_URI, 
       null, null, null, null);

String id = cur.getString(cur.getColumnIndex(Contacts._ID));

if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
         final Uri uri = RawContactsEntity.CONTENT_URI;
            final String selection = Data.CONTACT_ID + "=?";
            final String[] selectionArgs = new String[] {id};

            final Map<String, List<ContentValues>> contentValuesListMap =
                new HashMap<String, List<ContentValues>>();
            EntityIterator entityIterator = null;

            entityIterator = RawContacts.newEntityIterator(cr.query(
                    uri, null, selection, selectionArgs, null));

            while (entityIterator.hasNext()) {
                Entity entity = entityIterator.next();
                for (NamedContentValues namedContentValues : entity.getSubValues()) {
                    ContentValues contentValues = namedContentValues.values;
                    String key = contentValues.getAsString(Data.MIMETYPE);
                    if (key != null) {
                        List<ContentValues> contentValuesList =
                                contentValuesListMap.get(key);
                        if (contentValuesList == null) {
                            contentValuesList = new ArrayList<ContentValues>();
                            contentValuesListMap.put(key, contentValuesList);
                        }
                        contentValuesList.add(contentValues);
                    }
                }
            }
            Log.i(tag, "Contact index=" + id);
        } 
    }

Can somebody please tell me what's wrong with my code.

like image 600
Andy Voot Avatar asked Dec 19 '10 06:12

Andy Voot


2 Answers

After you execute query, you must call cur.moveToFirst()...

Try this

ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(Contacts.CONTENT_URI, 
       null, null, null, null);

if(cur != null && cur.moveToFirst())
{
       String id = cur.getString(cur.getColumnIndex(Contacts._ID));

        if (cur.getCount() > 0) {
         ...
like image 199
st0le Avatar answered Oct 22 '22 15:10

st0le


It is not required to call moveToFirst but to ensure that you moved to any of the returned rows before accessing a value (see moveToLast(), moveToPosition(int position), ... ).

So this one:

Cursor cur = cr.query(Contacts.CONTENT_URI, null, null, null, null);

while (cur.moveToNext()) {
    String id = cur.getString(cur.getColumnIndex(Contacts._ID));
    final Uri uri = RawContactsEntity.CONTENT_URI;

would work as well - or even better, because there is an issue with the accepted answer, if you continue with ...

while (cur.moveToNext()) {

after

if (cur.getCount() > 0) { 

It would skip the first row, what might not be obvious.

like image 2
Trinimon Avatar answered Oct 22 '22 15:10

Trinimon