多个联系人选择器列表[getCheckedItemPositions()]

时间:2011-02-12 06:26:13

标签: android

下面是我的Contact_Picker类。我将使用此类创建一个带复选框的联系人列表,让用户可以选择从他们的电话簿中选择多个联系人。我有一个我正在使用的布局xml,底部有2个按钮:全部清除和完成。

当按下“完成”时,我需要它来获取所有已检查的名称,并将它们保存在列表/首选项文件中。现在,我可以找到POSITIONS被检查的内容,但我不知道如何检索与它们相关的相应信息(所选联系人的姓名/电话号码)。我已经搜索了一个可行的方法,并且没有提出任何建议。非常感谢任何代码/伪代码/想法。

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

4 个答案:

答案 0 :(得分: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;
}

在这段代码中,我试图在字符串表中获取联系人列表,然后你可以轻松使用它

答案 1 :(得分: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));
}

答案 2 :(得分: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);
                }
            }

以防其他人遇到多选列表视图的问题。

答案 3 :(得分:0)

这是一个正确的方法:

SparseBooleanArray selectedPositions = listView.getCheckedItemPositions();

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