导入联系人仅导入联系人列表中的第一个联系人号码

时间:2014-12-08 08:43:31

标签: java android contact

这段代码似乎很好。我在许多网站上进行了研究,但是当我运行代码时,只有联系人列表中的第一个数字才会进入文本视图。但我希望在编辑文本视图中显示所需的联系号码。

private void importContact() {

    Intent importContactIntent = new Intent(Intent.ACTION_PICK);
    importContactIntent.setType(ContactsContract.Contacts.CONTENT_TYPE);
    startActivityForResult(importContactIntent, PICK_CONTACT);
}

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);
    switch (reqCode) {
    case (PICK_CONTACT):

        if (resultCode == Activity.RESULT_OK) {
            //Uri contactData = data.getData();
            cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
                    null, null);
            cursor.moveToNext();
                String name = cursor
                        .getString(cursor
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                destinationPhoneNumber = cursor
                        .getString(cursor
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                //
                enterPhoneNumber.setText(destinationPhoneNumber);
                Toast.makeText(this,
                        name + " has number " + destinationPhoneNumber,
                        Toast.LENGTH_LONG).show();

            cursor.close();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您可以像这样查询以获取特定联系人的所有号码:

String id = cur.getString(cur
  .getColumnIndex(ContactsContract.Contacts._ID));
String name = cur
  .getString(cur
    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

image_uri = cur
  .getString(cur
    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
if (Integer
  .parseInt(cur.getString(cur
    .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
 //System.out.println("name : " + name + ", ID : " + id);

 // NOW query all numbers of that particulat contact using contact_Id
 Cursor pCur = getContentResolver().query(
   ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
   null,
   ContactsContract.CommonDataKinds.Phone.CONTACT_ID
     + " = ?", new String[] { id }, null);
 while (pCur.moveToNext()) {
 // you can store phone in a arrayList
  phone = pCur
    .getString(pCur
      .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
  //System.out.println("phone" + phone);
 }
 pCur.close();
 }