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

前端 未结 15 1972
北海茫月
北海茫月 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条回答
  •  猫巷女王i
    2020-11-29 06:27

    while I do not believe I have tried every variation described here, here is the one that has worked for me :)

            @Override
            public View getView(final int position, View convertView, ViewGroup parent) 
            {
    
                CheckedTextView retView = (CheckedTextView) convertView;
    ...
                retView.setOnClickListener(new View.OnClickListener() 
                {
                    public void onClick(View v)
                    {
                        CheckedTextView chkVw = (CheckedTextView) v; 
    //                  chkVw.toggle();
    //                  chkVw.setChecked(!chkVw.isChecked());
                        mLstVwWordingSets.setItemChecked(position + 1, !chkVw.isChecked());
                    }
                });
    ...
           }
    

    And later

            SparseBooleanArray checkedItemsArray = mLstVwWordingSets.getCheckedItemPositions();
            for (int i = 1; i <  mLstVwWordingSets.getCount(); i++)         //skip the header view
            {
                if (checkedItemsArray.get(i, false))
                    Log.d(_TAG, "checked item: " + i);
            }
    

    I am accessing position + 1 due to a header view that my list has in place.

    HTH

提交回复
热议问题