获取字符串数组的联系人

时间:2014-08-31 16:35:26

标签: java android

我试图将所有联系人(姓名和电话号码)从我的手机转移到字符串数组。我尝试了这部分代码,但我得到了一个错误......

Cursor contacts = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
String aNameFromContacts[] = new String[contacts.getCount()];  
String aNumberFromContacts[] = new String[contacts.getCount()];

int ij = 0;

int nameFieldColumnIndex = contacts.getColumnIndex(PhoneLookup.DISPLAY_NAME);
int numberFieldColumnIndex = contacts.getColumnIndex(PhoneLookup.NUMBER);

while(contacts.moveToNext()) {
    String contactName = contacts.getString(nameFieldColumnIndex);
    aNameFromContacts[ij] = contactName ; 
    String number = contacts.getString(numberFieldColumnIndex);
    aNumberFromContacts[ij] = number ;
    ij++;
    contacts.close();

这是错误:

08-31 18:09:09.031: E/CursorWindow(16317): Failed to read row 0, column -1 from a CursorWindow which has 109 rows, 44 columns.

我该如何修复它还是有更好的方法吗?

1 个答案:

答案 0 :(得分:1)

始终使用contacts.getColumnIndexOrThrow(PhoneLookup.NUMBER);

而不是contacts.getColumnIndex(PhoneLookup.NUMBER);

这样您就可以获得确切的错误详情。在这种情况下是:

java.lang.IllegalArgumentException: column 'number' does not exist

表示您正在尝试获取不存在的列索引,因此您的getColumnIndex方法返回的值为null。

以下代码将为您提供所需内容:

ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
            null, null, null);
    String aNameFromContacts[] = new String[cur.getCount()];
    String aNumberFromContacts[] = new String[cur.getCount()];
    int j = 0;
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur
                    .getColumnIndexOrThrow(ContactsContract.Contacts._ID));
            String name = cur
                    .getString(cur
                            .getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));

            if (("1")
                    .equals(cur.getString(cur
                            .getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER)))) {
                Cursor pCur = cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = ?", new String[] { id }, null);
                int i = 0;
                int pCount = pCur.getCount();
                String[] phoneNum = new String[pCount];
                String[] phoneType = new String[pCount];
                while (pCur.moveToNext()) {
                    phoneNum[i] = pCur
                            .getString(pCur
                                    .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    phoneType[i] = pCur
                            .getString(pCur
                                    .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.TYPE));

                    i++;
                }
                if (phoneNum.length > 0) {
                    aNumberFromContacts[i] = phoneNum[0];
                    aNameFromContacts[j] = name;
                    Log.d("name = ", "" + name +", phoneNumber = " + phoneNum[0]);
                }
            }
            j++;
        }
    }

还尝试了解此代码,以便了解它的工作原理以及phoneNum和phoneType是什么。

PS:根据需要,可以优化此代码以获得更好的性能。