Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to implement a button that gets all checkbox's state and adds the value of checked item into arraylist?
    primarykey
    data
    text
    <p>I've searched around for a solution to my problem but couldn't find one so here it is.</p> <p>I have a arraylist of data that i display using a combination of textview and checkbox. This is done using custom adapter which basically inflates it row by row. Everything up to this point is fine until <strong>i want to retrieve the checked data</strong>. </p> <p>What i read from other people is that their checked boxes becomes unchecked when scrolled due to the recycling of the view. <strong>For me the state of the checkboxes remain</strong> but i don't know how to retireve them without scrolling it. When i check a box it is "check" on the UI but actaully it is not recognized until i scroll the checked item out of view and back in again. </p> <p>Currently i use a global arraylist to populate with list.get(position).getName() if the list.get(position).isSelected() == true. But you see this arraylist remains empty unless i scroll the item out of view and back again. the same applies when the boxes are unchecked, they remain in the arraylist until i scroll. What i have in mind is a submit button that takes all the checkbox's current state when it is clicked then construct the arraylist.</p> <p>any idea how to create such thing?</p> <pre><code>public View getView(int position, View convertView, ViewGroup parent) { View view = null; if (convertView == null) { LayoutInflater inflator = context.getLayoutInflater(); view = inflator.inflate(R.layout.qlist, null); final ViewHolder viewHolder = new ViewHolder(); viewHolder.text = (TextView) view.findViewById(R.id.label); viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check); viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { Model element = (Model) viewHolder.checkbox.getTag(); // if(isChecked){ selctionCount++; } else if (!isChecked){ selctionCount--; } if(selctionCount &gt; 2) { Toast.makeText(context, "error, you checked more than 2!", Toast.LENGTH_LONG).show(); element.setSelected(false); } else { element.setSelected(buttonView.isChecked()); } System.out.println(selctionCount); } }); view.setTag(viewHolder); viewHolder.checkbox.setTag(list.get(position)); } else { view = convertView; ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position)); } ViewHolder holder = (ViewHolder) view.getTag(); holder.text.setText(list.get(position).getName()); //This is the problem here! the selected arraylist (global) is empty even when i check the boxes, //it only populates when i scroll the checked boxes out of view and back in! helphelp! holder.checkbox.setChecked(list.get(position).isSelected()); if (list.get(position).isSelected() == true){ selected.add(list.get(position).getName()); } else if (list.get(position).isSelected() == false){ selected.remove(list.get(position).getName()); } return view; } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload