2012-09-03 82 views
0

我想從Android聯繫人列表中獲取數據。 我得到了display_name而不是數字。 我用這個代碼:獲取顯示的名稱,但不是Android中的號碼聯繫人列表

while(people.moveToNext()){ 
      try{ 
       int nameFieldColumnIndex = people.getColumnIndex(Phone.DISPLAY_NAME); 
       String name = people.getString(nameFieldColumnIndex); 
       try{ 
        int numberFieldColumnIndex = people.getColumnIndex(Phone.NORMALIZED_NUMBER); 
        String number = people.getString(numberFieldColumnIndex); 
        HashMap<String,String> contactMap=new HashMap<String, String>(); 
        contactMap.put("name", name); // per la chiave image, inseriamo la risorsa dell immagine 
        contactMap.put("number",number); // per la chiave name,l'informazine sul nome 
        data.add(contactMap); //aggiungiamo la mappa di valori alla sorgente dati 
       }catch(IllegalStateException e){e.printStackTrace();} 

,我嘗試:

Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; 
     String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, 
         ContactsContract.CommonDataKinds.Phone.NUMBER}; 

     Cursor people = getContentResolver().query(uri, projection, null, null, null); 

,但結果是一樣的,我得到了顯示的名稱而不是數量。 我讀了一些有用的帖子,但我沒有完成。 建議?

在此先感謝!

回答

0

自己解決。 我需要編碼一個單獨的查詢,以獲得與每個聯繫人相關的號碼:

ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>(); 

      if (cur.getCount() > 0) { 
      while (cur.moveToNext()) { 
       id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); 
       name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
       if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 

         if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 
          Cursor pCur = cr.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)); 
           HashMap<String,String> contactMap=new HashMap<String, String>(); 
           contactMap.put("name", name); // per la chiave image, inseriamo la risorsa dell immagine 
           contactMap.put("number",number); // per la chiave name,l'informazine sul nome 
           data.add(contactMap); //aggiungiamo la mappa di valori alla sorgente dati 
          } 
          pCur.close(); 
         } 
      } 
      } 

    } 
     cur.close(); 
相關問題