取出所有联系号码和其他号码

时间:2015-03-23 12:54:34

标签: android contactscontract

我想知道所有联系人号码+替代号码。

首先使用了这段代码:

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();

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

1 个答案:

答案 0 :(得分:0)

请使用此:

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);
}

}