0

我一直在按照这个网站的教程来获取 Android 中联系人的电话号码。

我面临的问题是,我似乎无法获得最后一个联系号码。例如,如果我保存了 3 个联系人,它会显示前 2 个联系人的号码。

如果它对任何人有帮助,我也会发布我的代码。

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){
                pCur=getContentResolver().
                        query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
                                ContactsContract.CommonDataKinds.Phone._ID+"=?", 
                                new String[]{id}, null);
                while(pCur.moveToNext()){
                    number.add(pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
                }                   
            }
            pCur.close();
        }
    }

任何帮助,将不胜感激。谢谢!

==============================

编辑:对于那些感兴趣的人,下面是对我有用的代码:

Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
    while (phones.moveToNext())
    {
      String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
      String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
      Log.i("Details", "Name: "+name+"  Number: "+phoneNumber);

    }
    phones.close();
4

2 回答 2

0

您好尝试以下代码获取所有联系人

        Cursor c=dbHelper.getAllContact();
        startManagingCursor(c);
        ArrayList<String> columnArray1 = new ArrayList<String>(); 
        if (c.moveToFirst())
        {
            do {          
                columnArray1.add(c.getString(0));
            } while (c.moveToNext());
        }
于 2013-03-04T08:53:42.883 回答
0
String[] projection = new String[]{ContactsContract.CommonDataKinds.Phone._ID,
                                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                                    ContactsContract.CommonDataKinds.Phone.NUMBER};
    String selection = ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER;
                        + 
    String[] selectionArgs = new String[] { "1" };                            
    Cursor c =  getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                                        projection, 
                                        selection, 
                                        selectionArgs, null}
if (c.moveToFirst())
{
     do
     {
        Log.d("Test", "Contact: id = " + c.getLong(0) + " name = " + c.getString(1) + " phone = " + c.getString(2));
     }
}
于 2013-03-04T08:02:29.163 回答