如果我选择联系人,我没有得到我的意思

问题描述:

我知道如何获得Android上的所有联系人,但是如果我选择联系人,我无法获得联系人所需的联系人。例如:我有4个接触如果我选择联系人,我没有得到我的意思

joe have phone number 7889 987; 
erick have phone number 8792 871; 
nona phone number 3653 872 and 2345 907; 
rina phone number 8796 235; 

如果我选择乔,我得到NONA的电话号码= 2345 907 我不知道什么是对我的应用程序的问题。

这是我的代码

public void tambahPenerima (View v) { 
    Intent i = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI); 
    i.setType(ContactsContract.Contacts.CONTENT_TYPE); 
    startActivityForResult(i, PICK_CONTACT);  
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if(resultCode == RESULT_OK) { 
      Cursor cursor = null; 
      String name = ""; 
      String number = ""; 

      cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); 

      while(cursor.moveToNext()) { 
       name = cursor.getString(cursor.getColumnIndex((ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME))); 
       number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
      } 
       if(cursor!=null) { 
        cursor.close(); 
       } 

       Intent kirimPesan = new Intent(); 
       kirimPesan.setClass(this, kirimPesan.class); 
       kirimPesan.putExtra("nama", name); 
       kirimPesan.putExtra("nomor", number); 
       kirimPesan.putExtra("chiper", chiper); 
       startActivity(kirimPesan); 
    }     
} 

有人请帮助我,我真的需要帮助。 对不起,我英文很差。 感谢..

 cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); 
if(cursor != null && cursor.moveToFirst()) { 

       while(cursor.moveToNext()) { 
        name = cursor.getString(cursor.getColumnIndex((ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME))); 
        number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
       } 
} 
+0

我曾尝试乌尔建议,但我的输出与我上面的代码相同。还有什么建议?请.. – androidDev 2013-03-12 12:29:45

这或许可以帮助你得到具体联系方式

这里姓名或号码通并获得联系方式至极你想

private String getContactName(Context context, String number) { 
     String name = null; 
     // define the columns I want the query to return 
     String[] projection = new String[] { 
       ContactsContract.PhoneLookup.DISPLAY_NAME, 
       ContactsContract.PhoneLookup._ID}; 

     // encode the phone number and build the filter URI 
     Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number)); 

     // query time 
     Cursor cursor = context.getContentResolver().query(contactUri, projection, null, null, null); 

     if(cursor != null) { 
      if (cursor.moveToFirst()) { 
       name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME)); 
       Log.v(TAG, "Started uploadcontactphoto: Contact Found @ " + number);    
       Log.v(TAG, "Started uploadcontactphoto: Contact name = " + name); 
      } else { 
       Log.v(TAG, "Contact Not Found @ " + number); 
      } 
      cursor.close(); 
     } 
     return name; 
    } 
+0

我已修改我的代码作为你的。但我不知道如何在意图中使用“名称”和“数字”。你能帮助我吗?因为我需要“名称”和“数字”在我的不同活动上插入到TextView上。谢谢 .. – androidDev 2013-03-12 12:29:21