多触点选择器列表[getCheckedItemPositions()]触点、选择器、列表、getCheckedItemPositions

2023-09-05 08:26:40 作者:相濡以沫

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

在完成是pressed,我需要它来获取所有被选中的名字,并保存在一个列表/ preferences文件。现在,我能找到什么样的立场进行检查,但我不知道如何获取与之相关的相应信息(选中联系人的姓名/电话号码)。我已经寻找上,将工作,并没有拿出什么方法日子。任何code /伪code /想法是极大的AP preciated。

Contact_Picker类:​​

 公共类Contact_Picker扩展ListActivity {

    受保护的静态最终字符串变量= NULL;
    公众的String []与我们联系= {};
    公众诠释[]到= {};
    公众的ListView myListView;




    / **第一次创建活动时调用。 * /
        @覆盖
        公共无效的onCreate(包savedInstanceState){
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.contacts_list);

            最终按钮done_Button =(按钮)findViewById(R.id.done_Button);
            最终按钮clear_Button =(按钮)findViewById(R.id.clear_Button);

            光标mCursor = getContacts();
            startManagingCursor(mCursor);

            ListAdapter适配器=新SimpleCursorAdapter(这一点,android.R.layout.simple_list_item_multiple_choice,mCursor,
                    联系方式=新的String [] {} ContactsContract.Contacts.DISPLAY_NAME,
                    为=新INT [] {android.R.id.text1});
            setListAdapter(适配器);
            myListView = getListView();
            myListView.setItemsCanFocus(假);
            myListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);



            clear_Button.setOnClickListener(新View.OnClickListener(){
                公共无效的onClick(视图v){
                    Toast.makeText(getApplicationContext(),选择清除,Toast.LENGTH_SHORT).show();
                    ClearSelections();
                }
            });

            / **当完成按钮按下:** /
            done_Button.setOnClickListener(新View.OnClickListener(){
                公共无效的onClick(视图v){


                    Log.i(TAG:完成按钮选择:);
                    SparseBooleanArray checkedPositions = myListView.getCheckedItemPositions();
                    Log.i(TAG,经过位数:+ checkedPositions.size());
                    如果(checkedPositions!= NULL)
                    {
                        诠释计数= myListView.getCount();
                        的for(int i = 0; I<计数;我++)
                        {
                            Log.i(TAG,选择的项目:+ checkedPositions.get(一));
                        }
                    }

                }
            }); //<  -  Done_Button结束

        } //<  - 的onCreate结束();


        私人无效ClearSelections(){

            诠释计数= this.myListView.getAdapter()getCount将()。

            的for(int i = 0; I<计数;我++){
                this.myListView.setItemChecked(我,FALSE);
            }

        }

        私人光标getContacts(){
            //运行查询
            开放的我们的uri = ContactsContract.Contacts.CONTENT_URI;
            的String []投影=新的String [] {ContactsContract.Contacts._ID,
                    ContactsContract.Contacts.DISPLAY_NAME};
            串选择= ContactsContract.Contacts.IN_VISIBLE_GROUP +=
                    +(1)+';
            的String [] selectionArgs两个= NULL;
            字符串排序顺序= ContactsContract.Contacts.DISPLAY_NAME
                    +分页中局部ASC;

            返回managedQuery(URI,投影,选择,selectionArgs两个,
                    排序);
        } //<  -  getContacts结束();

}
 

将创建输出,例如:

  Sele02-12 01:25:09.733:INFO /(219):完成按钮选择:
1月2日至一十二日:25:09.743:INFO /(219):经过位数:2
1月2日至一十二日:25:09.743:INFO /(219):选择项目:真
1月2日至一十二日:25:09.743:INFO /(219):选择项目:假的
1月2日至一十二日:25:09.743:INFO /(219):选择项目:真
1月2日至一十二日:25:09.752:INFO /(219):选择项目:假的
 
JIRA中配置下拉列表选择器,怎么配置选项

解决方案

下面是一个正确的做法:

  SparseBooleanArray selectedPositions = listView.getCheckedItemPositions();

的for(int i = 0; I< selectedPositions.size();我++){
    如果(selectedPositions.get(selectedPositions.keyAt(I))==真){
        //做的东西
    }
}
 

Below is a my Contact_Picker class. I am going to be using this class to create a list of contacts with checkboxes, giving the user the option to select multiple contacts from their phonebook. I have a layout xml that I am using that has 2 buttons at the bottom: Clear All and Done.

When 'Done' is pressed, I need it to get all of the names that are checked, and save them in a list/preferences file. Right now, I can find what POSITIONS are checked, but I don't know how to retrieve the corresponding information associated with them (the name/phone number of the selected contact). I have searched for days on a method that will work, and have not come up with anything. Any code/pseudo code/ideas are greatly appreciated.

Contact_Picker Class:

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

}

Will Create Output Such As:

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

解决方案

Here is a correct approach:

SparseBooleanArray selectedPositions = listView.getCheckedItemPositions();

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