Checkboxes in android listview having problem

后端 未结 3 575
臣服心动
臣服心动 2020-12-17 06:18

I have a listview with custom BaseAdapter and each row contains a checkbox and three textviews. I am using Layoutinflater to inflate this row from a xml file. However, every

3条回答
  •  情书的邮戳
    2020-12-17 07:18

    the problem is definitely within your getView() method;

    Try something like this

    public View getView(int position, View convertView, ViewGroup parent) {
            View vu = convertView;
            ViewHolder vHolder = null;
            try {
                LayoutInflater inflater = (LayoutInflater) context
                        .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
                if (vu == null) {
                    vu = (View) inflater.inflate(R.layout.list_fr_req, null);
                    vHolder = new ViewHolder();
                    vHolder.checkbox = (CheckBox) vu.findViewById(R.id.my_ChkBox);
                    vu.setTag(vHolder);
                } else {
                    vHolder = (ViewHolder) vu.getTag();
                }
                vHolder.checkbox.setOnCheckedChangeListener(this);
    
                vHolder.checkbox.setId(position);
                vHolder.textView.setId(position);
    
                if (myList.get(position).getCheckedStatus())
                    vHolder.checkbox.setChecked(true);
                else
                    vHolder.checkbox.setChecked(false);
    
            } catch (Exception e) {
                Log.d("Exception in getview", e + "");
                e.printStackTrace();
            }
            return vu;
        }
    
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                list.get(buttonView.getId()).setCheckedStatus(true);
            } else {
                list.get(buttonView.getId()).setCheckedStatus(false);
            }
        }
    
        public static class ViewHolder {
            CheckBox checkbox;
            TextView textview;
        }
    

    Regards: N_JOY.

提交回复
热议问题