且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查所有复选框在ListView

更新时间:2022-10-21 09:23:25

您可以尝试以下操作:


  1. 添加适配器1布尔值属性和设置方法。

  2. 在要检查所有的复选框点击按钮。调用适配器方法来设置布尔值。并调用notifyDataSetChanged,这应该废止和重绘列表视图。

  3. 虽然重绘过程中,适配器的getView方法被调用。因此,在这个方法检查,如果布尔值设置为true或false,并相应地选中或取消选中该复选框。

我希望这个步骤的帮助。

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.

I Googled for solutions but nothing worked so far.

How can I solve this?

Here is the adapter:

public class AlumnoArrayAdapter<T> extends BaseAdapter{

    Context mContext;
    LayoutInflater mInflater;
    ArrayList<T> mList;
    ArrayList<Alumno> alumno;
    boolean verEstadisticas;
    SparseBooleanArray mSparseBooleanArray;

    public AlumnoArrayAdapter(Context context, ArrayList<T> list, ArrayList<Alumno> 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<T>();
        this.mList = list;
    }

    public ArrayList<T> getCheckedItems() {
        ArrayList<T> mTempArry = new ArrayList<T>();

        for(int i=0;i<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);          
        }
    };
}

If more code is needed please let me know.

Would appreciate any help possible.

Thanks in advance!

EDIT: Got a solution!, the modified adapter:

public class AlumnoArrayAdapter<T> extends BaseAdapter{

    Context mContext;
    LayoutInflater mInflater;
    ArrayList<T> mList;
    ArrayList<Alumno> alumno;
    boolean verEstadisticas;
    SparseBooleanArray mSparseBooleanArray;

    boolean checarTodo = false;

    public AlumnoArrayAdapter(Context context, ArrayList<T> list, ArrayList<Alumno> 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<T>();
        this.mList = list;
    }

    public ArrayList<T> getCheckedItems() {
        ArrayList<T> mTempArry = new ArrayList<T>();

        for(int i=0;i<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 < 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;
    }


}

You can try following:

  1. Add one boolean property in adapter and a method to set the same.
  2. On click of button where you want to check all the checkbox. Call the adapter method to set boolean value. And call notifyDataSetChanged, this should invalidate and redraw the list view.
  3. While redrawing process, getView method of adapter is called. So in this method check if boolean value is set to true or false and accordingly check or uncheck the check box.

I hope this steps help.