Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView.getCheckedItemPositions not able to return checked Items in SparseBooleanArray

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

                }


            }
        }



    });                 
}
like image 733
Shrinidhi Rao Avatar asked Nov 14 '22 21:11

Shrinidhi Rao


1 Answers

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.

like image 88
Siten Avatar answered Jan 08 '23 01:01

Siten