如何检查所有复选框在ListView [英] How to check all checkboxes in a ListView

查看:133
本文介绍了如何检查所有复选框在ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表,其中每行包含一个复选框和一个TextView,我也有一个按钮,这应该/不选中所有复选框在列表中,不过,我一直无法与复选框外实现这一目标在屏幕上,只与有形项目之一。如果我深知,这是因为不存在屏幕之外的项目,他们回收的滚动时创建新​​的行,导致空指针/输出指数errores的同时,尝试检查/进入屏幕之外的复选框。

我Google了解决方案,但没有到目前为止的工​​作。

我该如何解决呢?

下面是适配器:

 公共类AlumnoArrayAdapter< T>延伸BaseAdapter {    上下文mContext;
    LayoutInflater mInflater;
    ArrayList的< T> mList;
    ArrayList的< Alumno> alumno;
    布尔verEstadisticas;
    SparseBooleanArray mSparseBooleanArray;    公共AlumnoArrayAdapter(上下文的背景下,ArrayList的< T>名单,ArrayList的< Alumno> alumno,布尔verEstadisticas){        // TODO自动生成构造函数存根
        this.alumno = alumno;
        this.verEstadisticas = verEstadisticas;        this.mContext =背景;        mInflater = LayoutInflater.from(mContext);
        mSparseBooleanArray =新SparseBooleanArray();
        mList =新的ArrayList< T>();
        this.mList =清单;
    }    公众的ArrayList< T> getCheckedItems(){
        ArrayList的< T> mTempArry =新的ArrayList< T>();        的for(int i = 0; I< mList.size();我++){
            如果(mSparseBooleanArray.get(ⅰ)){
                mTempArry.add(mList.get(I));
            }
        }
        返回mTempArry;
    }    公众诠释的getCount(){
        返回mList.size();
    }    公共对象的getItem(INT位置){
        返回mList.get(位置);
    }    众长getItemId(INT位置){
        返回的位置;
    }    公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){        如果(convertView == NULL){
            convertView = mInflater.inflate(R.layout.row,NULL);
        }        TextView的tvTitle =(TextView中)convertView.findViewById(R.id.tvTitle);
        tvTitle.setText(mList.get(位置)的ToString());        复选框mCheckBox =(复选框)convertView.findViewById(R.id.chkEnable);
        mCheckBox.setTag((整数)位置);
        mCheckBox.setChecked(mSparseBooleanArray.get(位置));
        mCheckBox.setOnCheckedChangeListener(mCheckedChangeListener);        如果(verEstadisticas){
            mCheckBox.setVisibility(View.GONE);
        }        mCheckBox.setOnClickListener(新View.OnClickListener(){
            公共无效的onClick(视图v){
                复选框CB =(复选框)V;
                整数位置=(整数)cb.getTag();
                alumno.get(位置).setChecked(cb.isChecked());
            }
        });        返回convertView;
    }    OnCheckedChangeListener mCheckedChangeListener =新OnCheckedChangeListener(){        公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){
            mSparseBooleanArray.put((整数)buttonView.getTag(),器isChecked);
        }
    };
}

如果需要更多的code,请让我知道。

请问AP preciate任何可能的帮助。

在此先感谢!

编辑:得到了解决!修改后的适配器:

 公共类AlumnoArrayAdapter< T>延伸BaseAdapter {    上下文mContext;
    LayoutInflater mInflater;
    ArrayList的< T> mList;
    ArrayList的< Alumno> alumno;
    布尔verEstadisticas;
    SparseBooleanArray mSparseBooleanArray;    布尔checarTodo = FALSE;    公共AlumnoArrayAdapter(上下文的背景下,ArrayList的< T>名单,ArrayList的< Alumno> alumno,布尔verEstadisticas){        // TODO自动生成构造函数存根
        this.alumno = alumno;
        this.verEstadisticas = verEstadisticas;        this.mContext =背景;        mInflater = LayoutInflater.from(mContext);
        mSparseBooleanArray =新SparseBooleanArray();
        mList =新的ArrayList< T>();
        this.mList =清单;
    }    公众的ArrayList< T> getCheckedItems(){
        ArrayList的< T> mTempArry =新的ArrayList< T>();        的for(int i = 0; I< mList.size();我++){
            如果(mSparseBooleanArray.get(ⅰ)){
                mTempArry.add(mList.get(I));
            }
        }
        返回mTempArry;
    }    公众诠释的getCount(){
        返回mList.size();
    }    公共对象的getItem(INT位置){
        返回mList.get(位置);
    }    众长getItemId(INT位置){
        返回的位置;
    }    公共无效checkAll(){
        为(中间体X = 0; X&下; alumno.size(); X ++){
            alumno.get(X).setChecked(alumno.get(X).isChecked()!);
        }
    }    公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){        如果(convertView == NULL){
            convertView = mInflater.inflate(R.layout.row,NULL);
        }        TextView的tvTitle =(TextView中)convertView.findViewById(R.id.tvTitle);
        tvTitle.setText(mList.get(位置)的ToString());        复选框mCheckBox =(复选框)convertView.findViewById(R.id.chkEnable);
        mCheckBox.setTag((整数)位置);
        mCheckBox.setChecked(alumno.get(位置).isChecked());        如果(verEstadisticas){
            mCheckBox.setVisibility(View.GONE);
        }        mCheckBox.setOnClickListener(新View.OnClickListener(){
            公共无效的onClick(视图v){
                复选框CB =(复选框)V;
                // Alumno Alumno =(Alumno)cb.getTag();
                整数位置=(整数)cb.getTag();
                alumno.get(位置).setChecked(cb.isChecked());
            }
        });        返回convertView;
    }
}


解决方案

您可以尝试以下操作:


  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.

这篇关于如何检查所有复选框在ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆