2014-12-08 131 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