2017-02-01 46 views
0

我有一個應用程序,我想要獲取單個聯繫人的多個號碼的項目點擊。 我有一個聯繫人列表視圖,其中我想要多個聯繫人的號碼,如果聯繫人有多個號碼。 這裏是我的代碼, 我得到所有的數字,但不能在Click上獲得多個數字。如何在android中編程查找單個聯繫人的所有數字?

MainActivity.java

ContentResolver cr = getContentResolver(); 
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, 
      null, null, null); 
    String phone = null; 
    String emailContact = null; 
    String emailType = null; 
    String image_uri = ""; 
    Bitmap bitmap = null; 
    if (cur.getCount() > 0) { 
     while (cur.moveToNext()) 
     { 
      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); 

       Cursor pCur = cr.query(
         ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
         null, 
         ContactsContract.CommonDataKinds.Phone.CONTACT_ID 
           + " = ?", new String[]{id}, null); 
       while (pCur.moveToNext()) 
       { 
        phone = pCur 
          .getString(pCur 
            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

        String phone1 = pCur 
          .getString(pCur 
            .getColumnIndex(String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE))); 
         Log.e("PHONE",phone1); 
        System.out.println("phone" + phone); 
       } 
       pCur.close(); 

       Cursor emailCur = cr.query 
         (
         ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
         null, 
         ContactsContract.CommonDataKinds.Email.CONTACT_ID 
           + " = ?", new String[]{id}, null); 
       while (emailCur.moveToNext()) 
       { 
        emailContact = emailCur 
          .getString(emailCur 
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 


if(TextUtils.isEmpty(emailContact)||emailContact.equalsIgnoreCase(null)|| 
emailContact.equalsIgnoreCase("")){ 
         emailContact=""; 

         Log.e("isEmpty","isEmpty " + emailContact); 
        }else{ 
         Log.e("gfdszfg","Email " + emailContact); 
        } 
        /* emailType = emailCur 
          .getString(emailCur 
            .getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));*/ 

        Log.e("gfdszfg","Email " + emailContact); 

       } 

       emailCur.close(); 
      } 

      if (image_uri != null) { 
       System.out.println(Uri.parse(image_uri)); 
       try { 
        bitmap = MediaStore.Images.Media 
          .getBitmap(this.getContentResolver(), 
            Uri.parse(image_uri)); 

        System.out.println(bitmap); 

       } catch (FileNotFoundException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 

      mList.add(new Contacts(name, phone, image_uri,emailContact)); 
      emailContact=""; 
     } 
     cur.close(); 
     mMyContactsAdapter = new MyContactsAdapter(MainActivity.this, 
    mList); 
     mcontact.setAdapter(mMyContactsAdapter); 
    } 
} 
+0

請參閱http://stackoverflow.com/a/26820544/2252830 – pskink

回答

0

好像你通過正確所有的手機都爲每個聯繫人迭代,但是當while循環結束,phone僅指向最後一個電話你發現。

... 
List<String> phonesList = new ArrayList<String>(); 
while (pCur.moveToNext()) { 
    phone = pCur.getString(... Phone.NUMBER)); 
    // add the phone to a growing list of phones 
    phonesList.add(phone); 
} 
pCur.close(); 
... 
// Change 'Contacts' constructor to accept List<String>, rather then String for phone 
mList.add(new Contacts(name, phonesList, image_uri,emailContact)); 
... 

當然,你可以做的電子郵件一樣的,如果你想在手機的標籤太多,你需要對列表:List<Pair<String, String>>保持對手機+標籤。

在一個側面說明這個代碼的性能,可以通過查詢ContactsContract.Data.CONTENT_URIALL電子郵件和電話,在設備上加以改進significantly,把它們放在一個HashMap,然後再遍歷所有聯繫人,並獲得電子郵件和電話,從HashMap。 這會將查詢數量從<number of contacts>*2減少到2

+0

我如何獲取聯繫人的多個號碼,當我點擊我的列表視圖時@marmor –

+0

如果您按照上面的建議設置了初始聯繫人列表,那麼您會有一個'聯繫人'對象列表,每個對象都包含一個電話列表。所以當一個項目被點擊時,你不需要從數據庫中查詢任何東西,只需從點擊的「聯繫人」對象中獲取電話列表 – marmor

相關問題