取出所有的联系电话和其他号码



我拥有取出所有联系人号码+备用号码的权限。

在首次使用此代码时:

Cursor phones = _context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (phones.moveToNext())
{
    Log.d("states","name: "+ phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)) +" - number: "+ phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();

但在这个代码中,如果一个名字有3个数字,只显示第三个数字。

然后使用了这个代码:

Cursor phones = _context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (phones.moveToNext())
{
    String id = phones.getString(phones.getColumnIndex(ContactsContract.Contacts._ID));
    String name = phones.getString(phones.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

    if (Integer.parseInt(phones.getString(phones.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 
    {
        Log.d("states","name : " + name + ", ID : " + id);
        Cursor pCur = _context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                                           null, 
                                           ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
        Log.d("states",pCur.getCount()+""); \ in here I always get 0 !!
         while (pCur.moveToNext())
         {
            String phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            Log.d("states","phone" + phone);
         }
         pCur.close();
    }
}
phones.close();

但这段代码根本没有给我任何数字:(

请使用此:

public class MainActivity extends Activity {
private ArrayList<FriendBean> arr = new ArrayList<FriendBean>();
private FriendAdapter friendAdapter;
private ListView mList;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mList = (ListView) findViewById(R.id.listView1);
    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
            null, null, null);
    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) {
                Cursor pCur = cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = ?", new String[] { id }, null);
                while (pCur.moveToNext()) {
                    String phoneNo = pCur
                            .getString(pCur
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    FriendBean bean = new FriendBean(name, phoneNo);
                    arr.add(bean);
                }
                pCur.close();
            }
        }
    }
    friendAdapter = new FriendAdapter(MainActivity.this, R.layout.row, arr);
    mList.setAdapter(friendAdapter);
}
}

最新更新