2012-09-13 76 views
6

關於同一主題有很多討論,但在此處花費4個小時後,我找不到有效的說明或鏈接以使聯繫人選擇器與複選框關聯。安卓聯繫人選擇器與複選框

我有一個活動完成按鈕和listviewcheckbox。我設法正確顯示了聯繫人。現在我想將所選聯繫人電話號碼以bundle(我認爲是最好的方式)返回,以便我可以獲取onActivityResult()中的號碼列表。我不確定我遵循的方式是否正確。

這裏是我的代碼:

public class ContactPickerMulti extends ListActivity implements OnClickListener { 

    // List variables 
    public String[] Contacts = {}; 
    public int[] to = {}; 
    public ListView myListView; 

    Button save_button; 
    private TextView phone; 
    private String phoneNumber; 
    private Cursor cursor; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.contacts_multi); 

     // Initializing the buttons according to their ID 
     save_button = (Button) findViewById(R.id.contact_done); 

     // Defines listeners for the buttons 
     save_button.setOnClickListener(this); 

     Cursor mCursor = getContacts(); 
     startManagingCursor(mCursor); 

     ListAdapter adapter = new SimpleCursorAdapter(
       this, 
       android.R.layout.simple_list_item_multiple_choice, 
       mCursor, 
       Contacts = new String[] { ContactsContract.Contacts.DISPLAY_NAME }, 
       to = new int[] { android.R.id.text1 }); 

     setListAdapter(adapter); 
     myListView = getListView(); 
     myListView.setItemsCanFocus(false); 
     myListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 

    } 

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

     return managedQuery(uri, projection, selection, selectionArgs, 
       sortOrder); 
    } 

    public void onClick(View src) { 
     Intent i; 
     switch (src.getId()) { 
     case R.id.contact_done: 

      SparseBooleanArray selectedPositions = myListView 
        .getCheckedItemPositions(); 
      SparseBooleanArray checkedPositions = myListView 
        .getCheckedItemPositions(); 
      if (checkedPositions != null) { 
       for (int k = 0; k < checkedPositions.size(); k++) { 
        if (checkedPositions.valueAt(k)) { 
          String name = 
           ((Cursor)myListView.getAdapter().getItem(k)).getString(1); 
          Log.i("XXXX",name + " was selected"); 
         } 
       } 
      } 

      break; 
     } 

    } 
} 

我想作爲數組或列表發送號碼。做這個的最好方式是什麼?任何幫助或導致正確的道路是高度讚賞。

+0

請添加適配器代碼,所以我可以知道你如何獲得電話號碼。 –

+0

@Yul代碼更新..現在它將打印聯繫人的名稱:) 我只需要得到的電話號碼,而不是名稱;) 完成! –

回答

3

onClick使用此代碼:

long[] id = getListView().getCheckedItemIds();// i get the checked contact_id instead of position 
     phoneNumber = new String[id.length]; 
     for (int i = 0; i < id.length; i++) { 

      phoneNumber[i] = getPhoneNumber(id[i]); // get phonenumber from selected id 

     } 

     Intent pickContactIntent = new Intent(); 
     pickContactIntent.putExtra("PICK_CONTACT", phoneNumber);// Add checked phonenumber in intent and finish current activity. 
     setResult(RESULT_OK, pickContactIntent); 
     finish(); 

//

private String getPhoneNumber(long id) { 
    String phone = null; 
    Cursor phonesCursor = null; 
    phonesCursor = queryPhoneNumbers(id); 
    if (phonesCursor == null || phonesCursor.getCount() == 0) { 
     // No valid number 
     signalError(); 
     return null; 
    } else if (phonesCursor.getCount() == 1) { 
     // only one number, call it. 
     phone = phonesCursor.getString(phonesCursor 
       .getColumnIndex(Phone.NUMBER)); 
    } else { 
     phonesCursor.moveToPosition(-1); 
     while (phonesCursor.moveToNext()) { 

      // Found super primary, call it. 
      phone = phonesCursor.getString(phonesCursor 
        .getColumnIndex(Phone.NUMBER)); 
      break; 

     } 
    } 

    return phone; 
} 


private Cursor queryPhoneNumbers(long contactId) { 
    ContentResolver cr = getContentResolver(); 
    Uri baseUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, 
      contactId); 
    Uri dataUri = Uri.withAppendedPath(baseUri, 
      ContactsContract.Contacts.Data.CONTENT_DIRECTORY); 

    Cursor c = cr.query(dataUri, new String[] { Phone._ID, Phone.NUMBER, 
      Phone.IS_SUPER_PRIMARY, RawContacts.ACCOUNT_TYPE, Phone.TYPE, 
      Phone.LABEL }, Data.MIMETYPE + "=?", 
      new String[] { Phone.CONTENT_ITEM_TYPE }, null); 
    if (c != null && c.moveToFirst()) { 
     return c; 
    } 
    return null; 
} 

和活動的最後onActivityResult你開始PickContactsActivity

// TODO Auto-generated method stub 
    // super.onActivityResult(requestCode, resultCode, data); 

    if (resultCode == RESULT_OK) { 

     if (requestCode == Constants.REQUEST_CODE_PICK_CONTACT) { 


      if (data != null) { 

       String[] temp = data.getStringArrayExtra("PICK_CONTACT"); 

      } 
     } 

    } 

} 
+0

感謝您的回覆。非常感謝。順便說一句,我有錯誤,'phonesCursor = queryPhoneNumbers(id);'我應該在'queryPhoneNumbers(id)做什麼;' –

+0

這是你在'''''''getClickNumber(id [i] );'。我爲我的應用程序使用此代碼,所以我確保它運行。所以你只需要改變一點以適應你的需求 –

+0

我完全失去了某個地方,這裏是我目前的代碼http://pastebin.com/tdTFF6t 你可以請看看嗎? –

2

當使用startActivityForResult(newActivity)newActivity你必須後跟finish()關閉ActivitysetResult(RESULT_OK)通話。或者,您可以在setResult(RESULT_OK, intent)的電話中包含Intent。撥打電話setResult()將導致呼叫您執行onActivityResult(),您可以在其中處理Activity的結果。所以在你的情況下,你只需要創建一個Intenet並使用putExtra()方法之一添加你的數組列表。然後Intent將傳遞到onActivityResult(),您可以在那裏提取該信息。有關更多信息,請參閱IntentActivity

+0

感謝您的回覆。我已經更新了代碼,現在我可以得到檢查的項目位置。那麼我如何獲得聯繫信息?我的意思是我只有項目的位置,我如何查詢製作LIST?任何想法? –

+0

你可以得到任何類型的數據,那麼問題在哪裏?只需放入一個ArrayList或其他東西。 –