2011-02-12 81 views
4

以下是我的Contact_Picker類。我將使用此類創建具有複選框的聯繫人列表,從而使用戶可以從其電話簿中選擇多個聯繫人。我有一個佈局xml,我使用的底部有2個按鈕:全部清除並完成。多個聯繫人選擇器列表[getCheckedItemPositions()]

當'完成'被按下時,我需要它來獲取所有檢查的名稱,並將它們保存在列表/首選項文件中。現在,我可以找到檢查的位置,但我不知道如何檢索與它們相關的相應信息(所選聯繫人的姓名/電話號碼)。我已經搜索了一個可行的方法,並沒有提出任何事情。任何代碼/僞代碼/想法非常感謝。

Contact_Picker類:​​

public class Contact_Picker extends ListActivity { 

    protected static final String TAG = null; 
    public String[] Contacts = {}; 
    public int[] to = {}; 
    public ListView myListView; 




    /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.contacts_list); 

      final Button done_Button = (Button) findViewById(R.id.done_Button); 
      final Button clear_Button =(Button) findViewById(R.id.clear_Button); 

      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); 



      clear_Button.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        Toast.makeText(getApplicationContext(),"Selections Cleared", Toast.LENGTH_SHORT).show(); 
        ClearSelections(); 
       } 
      }); 

      /** When 'Done' Button Pushed: **/ 
      done_Button.setOnClickListener(new View.OnClickListener() { 
       public void onClick (View v){ 


        Log.i(TAG,":Done Button Selected:"); 
        SparseBooleanArray checkedPositions = myListView.getCheckedItemPositions(); 
        Log.i(TAG,"Number of Checked Positions: " + checkedPositions.size()); 
        if (checkedPositions != null) 
        { 
         int count = myListView.getCount(); 
         for (int i=0;i<count;i++) 
         { 
          Log.i(TAG,"Selected items: " + checkedPositions.get(i)); 
         } 
        } 

       } 
      }); //<-- End of Done_Button 

     } //<-- end of onCreate(); 


     private void ClearSelections() {    

      int count = this.myListView.getAdapter().getCount(); 

      for (int i = 0; i < count; i++) { 
       this.myListView.setItemChecked(i, false); 
      } 

     } 

     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.IN_VISIBLE_GROUP + " = '" 
        + ("1") + "'"; 
      String[] selectionArgs = null; 
      String sortOrder = ContactsContract.Contacts.DISPLAY_NAME 
        + " COLLATE LOCALIZED ASC"; 

      return managedQuery(uri, projection, selection, selectionArgs, 
        sortOrder); 
     } //<-- end of getContacts(); 

} 

將創建輸出,例如:

Sele02-12 01:25:09.733: INFO/(219): :Done Button Selected: 
02-12 01:25:09.743: INFO/(219): Number of Checked Positions: 2 
02-12 01:25:09.743: INFO/(219): Selected items: true 
02-12 01:25:09.743: INFO/(219): Selected items: false 
02-12 01:25:09.743: INFO/(219): Selected items: true 
02-12 01:25:09.752: INFO/(219): Selected items: false 
+0

任何幫助大師? – burmat 2011-02-12 20:13:39

回答

0

這是一個正確的做法:

SparseBooleanArray selectedPositions = listView.getCheckedItemPositions(); 

for (int i=0; i<selectedPositions.size(); i++) { 
    if (selectedPositions.get(selectedPositions.keyAt(i)) == true) { 
     //do stuff 
    } 
} 
0

也許你可以手動跟蹤您的聯繫人:

 Vector<String> names=new Vector<String>(); 
     private Cursor getContacts() {... 
      Cursor cur = managedQuery(uri, projection, selection, selectionArgs, 
        sortOrder); 
      int col = cur.getColumnIndex("display_name"); 
      while(cur.moveToNext()) 
        names.add(cur.getString(col)); 
       cur.moveToFirst(); 
      return cur; 
     } 

,然後將其輸出同步:

for (int i=0;i<count;i++) 
{ 
    Log.i(TAG,"Selected items: " + checkedPositions.get(i)); 
    Log.i(TAG,"Selected name: " + names.get(i)); 
} 
0

我忘了這篇文章,直到團隊Pannous留下了答案。我敢肯定,他們的方法將工作,但我最終用這個代替:

  SparseBooleanArray checked = myListView.getCheckedItemPositions(); 
      for (int i = 0; i < ContactsList.length; i++) { 
       Log.v(TAG, ContactsList[i] + ": " + checked.get(i)); //<-- Will print every contact with 'true' 
       if (checked.get(i) == true) { 
        Object o = getListAdapter().getItem(i); 
        String name = o.toString(); 
        WriteSettings(self, name); 
       } 
      } 

萬一其他人是否有一個多項選擇列表視圖中的一個問題。

+0

如果您檢查一個項目然後取消選中它,這將不起作用。你的方法會報告所有已經被檢查過的項目,即使它們現在不在了。看到我的答案。 – Buffalo 2012-07-12 15:13:08

1

看到這個

public String[] getlistcontacts() { 
    // TODO Auto-generated method stub 

    int i=0; 
    ContentResolver cr = getContentResolver(); 
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null,null, null, null); 
    Cursor pCur = cr.query(
       ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null , null); 
     int a= cur.getCount(); 
    String[] cttlist=new String[a+1]; 

    cur.moveToFirst(); 
    pCur.moveToFirst(); 
    for (int j=0; j<a;j++){ 
     int nm=cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); 
     //int nb=pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); 
     String name=cur.getString(nm); 
     int nb=pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); 

     String number=pCur.getString(nb); 
     cttlist[i]=name.concat("<;>").concat(number); 

     //Toast.makeText(PizzastimeActivity.this, "alkamkljziha"+name+":"+number, Toast.LENGTH_LONG).show(); 
     i++; 
     cur.moveToNext(); 
     pCur.moveToNext(); 
    } 

    return cttlist; 
} 
在此代碼

我試圖讓聯繫人的列表中字符串的表,然後ü可以使用它輕鬆地

相關問題