android.database.CursorIndexOutOfBoundsException:指数-1请求 [英] android.database.CursorIndexOutOfBoundsException: Index -1 requested

查看:176
本文介绍了android.database.CursorIndexOutOfBoundsException:指数-1请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想阅读所有使用RawContacts.entityIterator的接触,但我看到这个错误:

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

android.database.CursorIndexOutOfBoundsException:指数-1要求

android.database.CursorIndexOutOfBoundsException: Index -1 requested

以下是我的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);
        } 
    }

有人能告诉我什么地方错了我的code。

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

推荐答案

在执行查询,则必须调用 cur.moveToFirst() ...

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

试试这个

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

这篇关于android.database.CursorIndexOutOfBoundsException:指数-1请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆