找到基于名字的电话号码 - 安卓

时间:2012-03-28 12:52:41

标签: android

我有一个列表视图,其中包含联系人列表中的三个人。如何显示这些人的电话号码?

有人可以发一个代码吗?

    for (int i=0;i<list.size();i++)
    {
      Log.i("TAG","PHONE NO IS: "+ ......)---------phone number?

      //list.get(i).getName() - is the contact list persons name.

      CounterResolvercr=getCounterResolver();
      Cursor cur= cr.query(ContactsContact.CONTENT_URI,null,null,null);
       while (cur.moveToNext())
       {
        if (cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))== list.get(i).getName())

           {
               String id= cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID);
                Cursor pcur=cr.quesy(ContactsContact.CommonDataKinds.Phone.CONTENT_URI, null, ContactContract.CommonDataKInds.Phone.CONTACT_ID+"=?",new Strng[]{id},null);

                 while (pCur.moveToNext())
                  {
                  String number =     pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                 Log.d(TAG,number);//IS NOT PRINTING ANYTHING WHY?!!!
                   }
              pcur.close()
                   }
                  }
    }

1 个答案:

答案 0 :(得分:2)

这是:

static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
    ContactsContract.Contacts._ID,
    ContactsContract.Contacts.DISPLAY_NAME,
    ContactsContract.Contacts.STARRED,
    ContactsContract.Contacts.TIMES_CONTACTED,
    ContactsContract.Contacts.CONTACT_PRESENCE,
    ContactsContract.Contacts.PHOTO_ID,
    ContactsContract.Contacts.LOOKUP_KEY,
    ContactsContract.Contacts.HAS_PHONE_NUMBER,
};

String name_to_search = "John Doe";

String select = "(" + ContactsContract.Contacts.DISPLAY_NAME + " == \"" +name_to_search+ "\" )";
Cursor c = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, select, null, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
context.startManagingCursor(c);

if (c.moveToNext())
{
    String id = c.getString(0);
    ArrayList<String> phones = new ArrayList<String>();

    Cursor pCur = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
    while (pCur.moveToNext())
    {
        phones.add(pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
        Log.i("", name_to_search+ " has the following phone number "+ pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
    } 
    pCur.close();   
}

属于该联系人的电话号码将存储在ArrayList手机中。

问候。