2

我正在尝试检索存储在手机中的联系人的姓名和类型,但出现此异常。我之前得到了名字但得到了例外

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();
                    } 
                }
            }
        }
    }
}
4

3 回答 3

8

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

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

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

它必须工作

于 2011-11-09T12:09:55.067 回答
2

你不应该创建三个不同的游标和上下文解析器,使用相同的,但在循环中设置条件以获得你想要的,我会给你一个我自己写的方法。这将为您提供手机中具有电话号码的所有联系人,您将了解实现您的想法:

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;
}
于 2011-11-09T12:01:11.350 回答
1

After days of search I have finally got the answers..type and name get stored in data table under DATA2 column thus I queried data.content_uri and got the index of DATA2 column and its just working fine...I am getting both name and type.

于 2011-11-10T13:46:47.737 回答