2014-12-08 109 views
1

此代码看起来不错。我曾在很多网站上进行过研究,但是当我运行代码时,只有联系人列表中的第一个号码出现在文本视图中。但我希望在编辑文本视图中显示所需的联系人号码。导入联系人只导入联系人列表中的第一个联系人号码

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(); 
     } 
    } 
} 
+0

所以,你希望某个联系人的所有号码? – 2014-12-08 08:48:52

+0

你觉得ans有用吗? – 2014-12-08 09:30:54

+0

@MohammedAli如果有多个号码,然后让用户选择它。我只想单一号码... – 2014-12-08 10:09:28

回答

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(); 
} 
+0

光标pCur = cr.query ,这一行显示错误。 (cr不能解决) – 2014-12-08 10:21:36

+0

把'cr = getContentResolver();'使用它之前......' – 2014-12-08 10:23:24

+0

确定没有错误,但是当我运行这个应用程序并选择一个联系人时,它会导致错误。和应用程序崩溃。点击某个联系人后 – 2014-12-08 10:42:26

相关问题