ListView.getCheckedItemPositions not able to return checked Items in SparseBooleanArray

半城伤御伤魂 提交于 2020-04-10 18:09:30

问题


I m trying to read the contacts in a list with multiple checkboxes, but when i call the sparsebooleanarray..it just return false for all the list entries,..even for the one s checked ...I looked into this thread Why is ListView.getCheckedItemPositions() not returning correct values? ...But when i implement the addClickHandlerToCheckBox it force stops..this has been bugging me for 4 days..please any help..

 public void populateContactList() {
    // Build adapter with contact entries
    final Cursor cursor = getContacts();
    String[] fields = new String[] {
            ContactsContract.Data.DISPLAY_NAME
    };

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_entry, cursor,
            fields, new int[] {R.id.contactEntryText});
    mContactList.setAdapter(adapter);
    mContactList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);



    proceedButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {


            SparseBooleanArray checked=mContactList.getCheckedItemPositions();

            int len = mContactList.getCount();


            for(int i=0;i<len;i++)
           {

                if(checked.get(i)==true)
                {

                    String item =  mContactList.getAdapter().getItem(checked.keyAt(i)).toString();
                    edt.append(","+item);

                }


            }
        }



    });                 
}

回答1:


I think you should use List Adapter view.

It will give an onCheckedChanged listener, so you will also get the position.

Like...

holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int cupos = position+1;

            }
        });         

Now look the whole example:

Listview.setAdapter(new settingsBase(getApplicationContext()));

private class settingsBase extends BaseAdapter{

    public settingsBase(Context context) {
       layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return listview.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder ;
     holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int cupos = position+1;

            }
        });         


        return convertView;
    }

    class ViewHolder{
        TextView txtSettingname , txtShow ;
        RadioButton radioButton ;
    }       
}

Try this--it will help you to get the position and whether checked true or false.



来源:https://stackoverflow.com/questions/6352990/listview-getcheckeditempositions-not-able-to-return-checked-items-in-sparseboole

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!