【问题标题】:I am making an app and getting issue in check box selection我正在制作一个应用程序并在复选框选择中遇到问题
【发布时间】:2016-01-16 22:32:34
【问题描述】:

每当我取消选中任何一个复选框按钮时,最后一个复选框按钮也未选中,所以我很困惑理解主要问题是什么。我认为 它正在被回收。

private ArrayList<MusterRollAttendanceMaster> list;
    private LayoutInflater inflator;
    private ViewHolder holder = null;;

    public MusterGridAdapter(Activity context,
            ArrayList<MusterRollAttendanceMaster> list) {
        super(context, R.layout.row, list);
        this.list = list;
        inflator = context.getLayoutInflater();
    }

    @SuppressLint("ViewTag")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = inflator.inflate(R.layout.row, null);
            holder = new ViewHolder();
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        holder.chk = (CheckBox) convertView.findViewById(R.id.checkbox);

        holder.chk.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                int count = list.size();
                boolean isAllChecked = true;

                for (int i = 0; i < count; i++)
                    if (!(list.get(i).getSelected())) {
                        isAllChecked = false;
                    }

                holder.chk.setChecked(isAllChecked);

            }

        });

        holder.chk
                .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    public void onCheckedChanged(CompoundButton view,
                            boolean arg1) {
                        int getPosition = (Integer) view.getTag();
                        list.get(getPosition).setSelected(view.isChecked());
                        for (int i = 0; i < list.size(); i++) {
                            if (!list.get(i).getSelected())
                                checkBox_header.setChecked(false);
                        }
                        int p = 0;
                        for (int i = 0; i < list.size(); i++) {
                            if (list.get(i).getSelected())
                                p++;
                        }
                        if (p == list.size())
                            checkBox_header.setChecked(true);
                    }
                });

        convertView.setTag(holder);
        convertView.setTag(R.id.checkbox, holder.chk);
        String part2;
        holder.chk.setTag(position);
        holder.jcn.setText(part2);

        holder.chk.setChecked(list.get(position).isSelected());

        return convertView;
    }

    public class ViewHolder {
        private TextView jcn;
        private TextView applicant;
        private CheckBox chk;
    }

    public int getCount() {

        count = list.size();
        return count;
    }
}

【问题讨论】:

    标签: android


    【解决方案1】:

    它的问题是由 listview 的单元重用造成的。检查以下链接,这肯定会对您有所帮助

    http://www.lalit3686.blogspot.in/2012/06/today-i-am-going-to-show-how-to-deal.html

    Android Click on listItem checks wrong checkbox

    【讨论】:

      【解决方案2】:

      尝试做这样的事情:

      顶部尺寸:

      private ArrayList<String> checkedNames = new ArrayList<>();
      

      在您的膨胀视图中:

      if (checkedNames.contains(holder.chk.getText().toString()))
          holder.chk.setChecked(true);
      else
          holder.chk.setChecked(false);
      
      holder.chk.setOnCheckedChangeListener(new OnCheckedChangeListener()
      {
           @Override
          public void onCheckedChanged(CompoundButton buttonView,
                  boolean isChecked) {
             String name = buttonView.getText().toString(); //or declare your checkbox final
             if (isChecked)
                checkedNames.add(buttonView.getText().toString());
             else
                checkedNames.remove(buttonView.getText().toString());
          }
      });
      

      当你的 listview 项目膨胀时,它应该是:

       convertView = inflator.inflate(R.layout.row, parent, false);
      

      【讨论】:

        【解决方案3】:

        将模型类中的CheckBox 状态保存为布尔值。当您更改 CheckBox 状态(选中或未选中)时,还会更改模型类中的布尔值,只需调用 notifyDataSetChanged()。它会让适配器知道你已经改变了CheckBox 状态。快乐编码。

        【讨论】:

          猜你喜欢
          • 2022-12-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-08-25
          • 2021-10-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多