Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to check all checkboxes in a ListView
    primarykey
    data
    text
    <p>I have a list, in which each row contains a checkbox and a textview, also I have a button which should check/uncheck all checkboxes in the list, however, I have not been able to achieve this with the checkboxes OUTSIDE of the screen, only with the visibles one. And if I understood well, that happens because items outside of the screens dont exist, they are recycled to create the new rows while scrolling, causing null pointers/out of index errores while trying to check/access the checkboxes outside of the screen.</p> <p>I Googled for solutions but nothing worked so far.</p> <p>How can I solve this?</p> <p>Here is the adapter:</p> <pre><code>public class AlumnoArrayAdapter&lt;T&gt; extends BaseAdapter{ Context mContext; LayoutInflater mInflater; ArrayList&lt;T&gt; mList; ArrayList&lt;Alumno&gt; alumno; boolean verEstadisticas; SparseBooleanArray mSparseBooleanArray; public AlumnoArrayAdapter(Context context, ArrayList&lt;T&gt; list, ArrayList&lt;Alumno&gt; alumno, boolean verEstadisticas) { //TODO Auto-generated constructor stub this.alumno = alumno; this.verEstadisticas = verEstadisticas; this.mContext = context; mInflater = LayoutInflater.from(mContext); mSparseBooleanArray = new SparseBooleanArray(); mList = new ArrayList&lt;T&gt;(); this.mList = list; } public ArrayList&lt;T&gt; getCheckedItems() { ArrayList&lt;T&gt; mTempArry = new ArrayList&lt;T&gt;(); for(int i=0;i&lt;mList.size();i++) { if(mSparseBooleanArray.get(i)) { mTempArry.add(mList.get(i)); } } return mTempArry; } public int getCount() { return mList.size(); } public Object getItem(int position) { return mList.get(position); } public long getItemId(int position) { return position; } public View getView(final int position, View convertView, ViewGroup parent) { if(convertView == null) { convertView = mInflater.inflate(R.layout.row, null); } TextView tvTitle = (TextView) convertView.findViewById(R.id.tvTitle); tvTitle.setText(mList.get(position).toString()); CheckBox mCheckBox = (CheckBox) convertView.findViewById(R.id.chkEnable); mCheckBox.setTag((Integer) position); mCheckBox.setChecked(mSparseBooleanArray.get(position)); mCheckBox.setOnCheckedChangeListener(mCheckedChangeListener); if(verEstadisticas){ mCheckBox.setVisibility(View.GONE); } mCheckBox.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { CheckBox cb = (CheckBox) v; Integer position = (Integer) cb.getTag(); alumno.get(position).setChecked(cb.isChecked()); } }); return convertView; } OnCheckedChangeListener mCheckedChangeListener = new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { mSparseBooleanArray.put((Integer) buttonView.getTag(), isChecked); } }; } </code></pre> <p>If more code is needed please let me know.</p> <p>Would appreciate any help possible.</p> <p>Thanks in advance!</p> <p>EDIT: Got a solution!, the modified adapter:</p> <pre><code>public class AlumnoArrayAdapter&lt;T&gt; extends BaseAdapter{ Context mContext; LayoutInflater mInflater; ArrayList&lt;T&gt; mList; ArrayList&lt;Alumno&gt; alumno; boolean verEstadisticas; SparseBooleanArray mSparseBooleanArray; boolean checarTodo = false; public AlumnoArrayAdapter(Context context, ArrayList&lt;T&gt; list, ArrayList&lt;Alumno&gt; alumno, boolean verEstadisticas) { //TODO Auto-generated constructor stub this.alumno = alumno; this.verEstadisticas = verEstadisticas; this.mContext = context; mInflater = LayoutInflater.from(mContext); mSparseBooleanArray = new SparseBooleanArray(); mList = new ArrayList&lt;T&gt;(); this.mList = list; } public ArrayList&lt;T&gt; getCheckedItems() { ArrayList&lt;T&gt; mTempArry = new ArrayList&lt;T&gt;(); for(int i=0;i&lt;mList.size();i++) { if(mSparseBooleanArray.get(i)) { mTempArry.add(mList.get(i)); } } return mTempArry; } public int getCount() { return mList.size(); } public Object getItem(int position) { return mList.get(position); } public long getItemId(int position) { return position; } public void checkAll(){ for(int x = 0; x &lt; alumno.size(); x++){ alumno.get(x).setChecked(!alumno.get(x).isChecked()); } } public View getView(final int position, View convertView, ViewGroup parent) { if(convertView == null) { convertView = mInflater.inflate(R.layout.row, null); } TextView tvTitle = (TextView) convertView.findViewById(R.id.tvTitle); tvTitle.setText(mList.get(position).toString()); CheckBox mCheckBox = (CheckBox) convertView.findViewById(R.id.chkEnable); mCheckBox.setTag((Integer) position); mCheckBox.setChecked(alumno.get(position).isChecked()); if(verEstadisticas){ mCheckBox.setVisibility(View.GONE); } mCheckBox.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { CheckBox cb = (CheckBox) v; //Alumno Alumno = (Alumno) cb.getTag(); Integer position = (Integer) cb.getTag(); alumno.get(position).setChecked(cb.isChecked()); } }); return convertView; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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