android.database.cursor索引超出异常范围?

时间:2011-11-09 11:37:47

标签: android indexoutofboundsexception

我正在尝试检索手机中存储的联系人的姓名和类型,但是会收到此异常。我以前得到了名字,但得到了异常

  

android.database.CursorIndexOutOfBoundsException:请求索引为-1,大小为10

当试图一起检索名称和类型时。请帮忙。提前谢谢。

package application.test;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;

import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;
import android.util.Log;

public class TestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String[] projection = new String[] {ContactsContract.Contacts.DISPLAY_NAME};     
        String[] projection1=new String[]{Phone.TYPE};
        String[] projection2=new String[]{ContactsContract.Contacts._ID};

        ContentResolver cr = getContentResolver();
        ContentResolver ncr=getContentResolver();
        ContentResolver icr=getContentResolver();

        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, projection,null, null, Contacts.DISPLAY_NAME + " ASC");
        Cursor ncur=ncr.query(ContactsContract.Data.CONTENT_URI, projection1, null, null,null);
        Cursor icur = icr.query(ContactsContract.RawContacts.CONTENT_URI, projection2,null, null, Contacts._ID + " ASC");

        if (cur.getCount() >0 && ncur.getCount()>0 && icur.getCount()>0) 
        {
            while (cur.moveToNext()&& ncur.moveToNext()&& icur.getCount()>0) 
            {

                String id = icur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                String  type=ncur.getString(ncur.getColumnIndex(Phone.TYPE));

                if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 
                {
                    Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
                                                                 ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null);     
                    Cursor typecur = ncr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                                                                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null)          

                    while (pCur.moveToNext()&& typecur.moveToNext())
                    {

                        Log.d("names",name);
                        Log.d("types",type);
                        pCur.close();
                    } 
                }
            }
        }
    }
}

3 个答案:

答案 0 :(得分:8)

当你在阅读之前创建一个光标。将光标位置移动到第一位。

while (cur.moveToNext()&& ncur.moveToNext()&& icur.getCount()>0) 

  /* move icur to first position then read */
  icur.moveFirst()

必须有效

答案 1 :(得分:2)

你不应该创建三个不同的游标和contextresolver,使用相同的,但是在循环中创建条件以获得你想要的东西,我会给你一个我自己编写的方法。 这将为您提供手机中具有电话号码的所有联系人,您将了解如何实施您的联系人:

public ArrayList<Contact> getSMSContacts(Context context){
    ArrayList<Contact> contacts = new ArrayList<Contact>();
    ContentResolver cr = context.getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.Contacts.DISPLAY_NAME + " asc");
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                Contact c = new Contact();
                c.setId(Integer.parseInt(id));
                c.setName(name);
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                        new String[]{id}, null);
                while (pCur.moveToNext()) {
                    String number = pCur.getString(
                            pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    c.setNumber(number);
                } 
                pCur.close();
                contacts.add(c);
            }
        }
    }
    cur.close();
    return contacts;
}

答案 2 :(得分:1)

经过几天的搜索,我终于得到了答案..类型和名称存储在DATA2列下的数据表中,因此我查询了data.content_uri并获得了DATA2列的索引,它的工作正常...我正在获取名称和类型。