2013-02-22 255 views
0

我能夠使用以下代碼從聯繫人中檢索聯繫人姓名和號碼。我的問題是,當聯繫人有多個號碼時,它會以某種方式選擇其中一個號碼,我不想要。從一個有兩個以上聯繫號碼的聯繫人中只選擇一個號碼

如果一個聯繫人有多個電話號碼,我需要用戶能夠選擇他想要選擇的電話號碼。

這是我的代碼。

private void pickContacts() { 
       // TODO Auto-generated method stub 
       Intent it = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);   
       startActivityForResult(it,1); 
      } 

在onActivityResult(INT requestCode,INT發送resultCode,意圖數據)

if(requestCode==1){ 
     Uri contactData = data.getData(); 
     Cursor c = managedQuery(contactData, null, null, null, null); 
     if (c.moveToFirst()) 
     {      
      String id = c.getString(
        c.getColumnIndex(ContactsContract.Contacts._ID)); 
      name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
      localEditor.putString("contactName", name); 
      tv2.setText(name);  
      int hasPhone=c.getInt(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
      if(hasPhone==1){ 
       Cursor pCur = getContentResolver().query(
         ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
         null, 
         ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
         new String[]{id}, null); 
           while(pCur.moveToNext()){ 

            number =pCur.getString(pCur.getColumnIndex(
              ContactsContract.CommonDataKinds.Phone.NUMBER)); 
           // Toast.makeText(getApplicationContext(), number, Toast.LENGTH_LONG).show(); 

            localEditor.putString("contactNumber", number); 

           tv3.setText(number); 

           } 
           pCur.close(); 
      } 
     }while(c.moveToNext()); 




     localEditor.commit(); 
     // tv2.setText(name);  
    // tv3.setText(number); 
    super.onActivityResult(requestCode, resultCode, data); 
} 

感謝提前幫助..

+0

請按照以下鏈接,這將在您的情況 [http://stackoverflow.com/questions/6171328/selecting-a-number-from-user-with-multiple-numbers-when-using-幫助所述接觸選取器] [1] [1]:http://stackoverflow.com/questions/6171328/selecting-a-number-from-user-with-multiple-numbers-when-using -se-contact-picker – 2013-02-22 05:03:45

+0

這個問題已經在這裏解答了http://stackoverflow.com/questions/9798639/querying-all-phone-numbers-of-a-contact-solved – 2013-02-22 05:04:16

+0

thnks的幫助 – 2013-02-22 05:33:19

回答

1

Cursor有一個稱爲getCount方法,它返回的行數在遊標中。你可以用它來找出是否有多個號碼並列出來選擇一個號碼。

嘗試......

if (c.getInt(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 
    Cursor pCur = getContentResolver().query(
     ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
     null, 
     ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
     new String[]{id}, null); 
    if(pCur.getCount() > 1) { 
     int i=0; 
     String[] phoneNum = new String[pCur.getCount()]; 
     while (pCur.moveToNext()) { 
      // store the numbers in an array 
      phoneNum[i] = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
      i++; 
     } 
     // list the phoneNum array (perhaps using radiobuttons) & give the choice to select one number  
    } else { 
     // do what you are doing now 
     // while (pCur.moveToNext()) { 
     //}  
    } 
    pCur.close(); 
} 

希望這有助於。

+0

沒有看到上面關於qu的評論已經回答了!然而,將離開的答案,因爲它是... – neo108 2013-02-22 06:20:34

+0

http://stackoverflow.com/questions/15038975/unable-to-fetch-contact-having-single-name-and-number-into-textview-android – 2013-02-25 06:57:12

相關問題