Why is ListView.getCheckedItemPositions() not returning correct values?

前端 未结 15 1850
北海茫月
北海茫月 2020-11-29 06:01

The app has a ListView with multiple-selection enabled, in the UI it works as expected. But when I read the values out using this code:

Log.         


        
相关标签:
15条回答
  • 2020-11-29 06:17

    It happens if you do not choose the correct resource for showing your different items. It works fine if you choose the built-in resource android.R.layout.simple_list_item_multiple_choice. The method getCheckedItemPositions is coupled in some way to the built-in resource.

    0 讨论(0)
  • 2020-11-29 06:17

    This adjustment fixed it for me. Change the getView method so that "int position" is "final int position". Then do this with your checkbox:

    ((CheckBox) convertView).setOnCheckedChangeListener(new OnCheckedChangeListener() {
    
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        list.setItemChecked(position, ((CheckBox) buttonView).isChecked());
                    }
                });
    

    Let me elaborate. list is a reference to the listview and in my case the adapter is an inner class in the dialog holding the list.

    0 讨论(0)
  • 2020-11-29 06:23

    None of the above solutions have worked for me, instead I get every child (a checkedTextView) from the ListView and see if it is checked or not:

                ListView myListView = myViewActivity.getListView();
            ArrayList<String> selectedChildren2 = new ArrayList<String>();
    
            for(int i = 0;i<myListView.getChildCount();i++)
            {
                CheckedTextView c = (CheckedTextView) myListView.getChildAt(i);
                if(c.isChecked())
                {
                    String child = c.getText().toString();
                    selectedChildren.add(child);
                    }
            }
    
    0 讨论(0)
  • 2020-11-29 06:23

    I found this thread by having the same problem but I think I have come up with a workaround that worked for me for unkown reasons. Whenever I tried getting a value I got nothing but if I loop through the list setting all to false it started working just like intended.

    This was actually a feature I had implemented where the user could either "Select All" or "Unselect All". I run this method in my onCreate.

    private void selectNone() {
        ListView lv = getListView();
        for (int i = 0; i < lv.getCount(); i++) {
            lv.setItemChecked(i, false);
        }
    }
    

    Now all my values are correct. For getting the values, in my case, just Strings.

    private void importSelected() {     
        ListView lv = getListView();
        SparseBooleanArray selectedItems = lv.getCheckedItemPositions();
    
        for (int i = 0; i < selectedItems.size(); i++) {
            if (selectedItems.get(i)) {
                String item =  lv.getAdapter().getItem(selectedItems.keyAt(i)).toString();
            }
        }
    
        selectNone(); //Reset
    }
    

    I hope this helps someone.

    0 讨论(0)
  • 2020-11-29 06:23

    Isn't there a rather fundamental difference between 'selected' and 'checked'?

    Suspect you want to setItemChecked() from an OnItemSelectedListener...

    0 讨论(0)
  • 2020-11-29 06:24
    ArrayList<Integer> ar_CheckedList = new ArrayList<Integer>();
    

    for each holer of check box i am using to store it in array

    holder.chk_Allow.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if(isChecked)
                            ar_CheckedList.add(position);
                    }
                });
    

    on click of button

    for (int i = 0; i <  ar_CheckedList.size(); i++)
     {
            HashMap<String, String> temp=(HashMap<String, String>) contactList.get(ar_CheckedList.get(i));
            str_Phone_No=temp.get(TAG_CONTACT_MOBILE);
            send(str_Phone_No);
     }
    
    0 讨论(0)
提交回复
热议问题