2012-03-21 181 views
3

我設法編寫添加程序,當用戶單擊一個按鈕時,他將能夠檢索將填充editText框的電話號碼。問題是,如果一個聯繫人有多個號碼,它將始終佔用最多的號碼。查詢聯繫人的所有電話號碼

我一直在讀另一個線程,Getting Number from Contacts Picker,有一個答案,但我不明白。由於我是Android編程新手, 如果有人能給我一步一步的指示,我會很感激。

回答

2

首先,您可能需要查詢電話簿中的所有聯繫人。

 // Run query 
     Uri uri = ContactsContract.Contacts.CONTENT_URI; 
     String[] projection = new String[] { 
       ContactsContract.Contacts._ID, 
       ContactsContract.Contacts.DISPLAY_NAME 
     }; 
     String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'"; 
     String[] selectionArgs = null; 
     String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"; 

     // Build adapter with contact entries 
     Cursor mCursor = managedQuery(uri, projection, selection, selectionArgs, sortOrder); 
     // 
     // Bind mCursor to to your Listview 
     // 

之後,當用戶在列表視圖中選擇一個聯繫人時,您將進行第二個查詢以獲取該聯繫人的標籤和編號。

@Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     mCursor.moveToPosition(position); 
     startManagingCursor(mCursor);  
     String contactID = mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts._ID)); 

     Cursor phoneNumCursor = getContentResolver().query(Phone.CONTENT_URI, 
       null, Phone.CONTACT_ID + "=?", new String[] { contactID }, null); 

     phoneNumCursor.moveToFirst(); 

     Vector<String> phoneTypeList = new Vector<String>(); 
     Vector<String> phoneNumberList = new Vector<String>(); 

     while (!phoneNumCursor.isAfterLast()){ 
      int type = phoneNumCursor.getInt(phoneNumCursor.getColumnIndex(Phone.TYPE)); 
      phoneTypeList.add(String.valueOf(Phone.getTypeLabel(getResources(),type,""))); 
      phoneNumberList.add(phoneNumCursor.getString(phoneNumCursor.getColumnIndex(Phone.NUMBER))); 
      phoneNumCursor.moveToNext(); 
     } 
     // 
     // Feel free to show label and phone number of that contact. ^^ 
     // 

更新時間:

下面是一個例子,如果你想使用聯繫人選擇器。

private static final int CONTACT_PICKER_RESULT = 1001; 

    protected void startContactPicker(){ 
     Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI); 
     startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (resultCode == RESULT_OK) { 
      switch (requestCode) { 
      case CONTACT_PICKER_RESULT: 
       Cursor cursor = null; 
       String phoneNumber = ""; 
       try { 
        Uri result = data.getData(); 
        String id = result.getLastPathSegment(); 
        cursor = getContentResolver().query(Phone.CONTENT_URI, 
          null, Phone.CONTACT_ID + "=?", new String[] { id }, null); 

        int phoneIdx = cursor.getColumnIndex(Phone.DATA); 
        if (cursor.moveToFirst()) { 
         while (!cursor.isAfterLast()){ 
          phoneNumber = cursor.getString(phoneIdx); 
          // 
          // this will go through all phoneNumber of selected contact. 
          // 

          cursor.moveToNext(); 
         } 
        } 
       } catch (Exception e) { 
       } finally { 
        if (cursor != null) { 
         cursor.close(); 
        } 
        numberView.setText(phoneNumber); 

       } 

       break; 
      }   
     } 
    } 
+0

列表視圖的含義,我必須添加在XML? – Akiff 2012-03-21 06:11:54

+0

你可以把它放在XML中或通過java代碼來顯示所有聯繫人名稱的列表。 – PhatHV 2012-03-21 06:39:33

+0

謝謝你的回答!我試着慢慢地理解程序。我目前失去了 – Akiff 2012-03-21 07:33:20

相關問題