0

我需要将光标设置为我有其 ID 的特定联系人

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

我有所有数据库的代码,我该如何优化它?我需要光标返回联系人的姓名和默认号码

谢谢你

4

2 回答 2

0

你实际上需要两个游标。一个用于姓名和身份证,另一个用于电话号码。

 Cursor c = managedQuery(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

 if (c.moveToFirst()) {
    name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
    fullName  = c.getString(c.getColumnIndex("display_name_alt"));
    id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));

    if (Integer.parseInt(c.getString(c.getColumnIndex
                   (ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
        //phone number

        Cursor phones = getContentResolver().query(Phone.CONTENT_URI, 
                              null,
                              Phone.CONTACT_ID + " = " + id,
                              null, 
                              null);

        while (phones.moveToNext()) {
               String number_type = phones.getString(phones.getColumnIndex
                              (ContactsContract.CommonDataKinds.Phone.TYPE));
               if(number_type.equalsIgnoreCase("1"))
                    number1 = phones.getString(phones.getColumnIndex
                              (ContactsContract.CommonDataKinds.Phone.NUMBER));
               if(number_type.equalsIgnoreCase("2"))
                    number2 = phones.getString(phones.getColumnIndex
                              (ContactsContract.CommonDataKinds.Phone.NUMBER));
        }

        phones.close();
    }
 }
 c.close(); 
于 2012-08-06T10:00:02.660 回答
0
cursor = getContentResolver().query(Phone.CONTENT_URI,
                            null, Phone.CONTACT_ID + "=?", new String[] { id },
                            Phone.DISPLAY_NAME + " ASC");
于 2012-08-08T14:10:06.467 回答