【问题标题】:check box unchecked issue when i scroll the custom listview android当我滚动自定义列表视图 android 时复选框未选中的问题
【发布时间】:2016-12-22 07:58:06
【问题描述】:

具有多个复选框的列表视图行,当我选中复选框时,如果我滚动列表视图,它会自动取消选中复选框。如何解决这个问题。这是我的 getView 方法

        public View getView(final int position, View convertView, ViewGroup parent) {
            final QuestionRadioHolder questionRadioHolder;
            final QuestionCheckboxHolder questionCheckboxHolder;
            final Model rowItem = getItem(position);
            final int type = getItemViewType(position);

            if (type == TYPE1) {
                if (convertView == null) {
                    convertView = mInflater.inflate(R.layout.radio_layout, parent, false);
                    questionRadioHolder = new QuestionRadioHolder();
                    questionRadioHolder.txtQuestion = (TextView) convertView.findViewById(R.id.txtQue);
                    questionRadioHolder.rg = (RadioGroup) convertView.findViewById(R.id.rg);

                    Typeface face = Typeface.createFromAsset(getContext().getAssets(), "fonts/Raleway-SemiBold.ttf");
                    questionRadioHolder.txtQuestion.setTypeface(face);
                    convertView.setTag(questionRadioHolder);

                } else {
                    questionRadioHolder = (QuestionRadioHolder) convertView.getTag();
                }



                List<String> arrayList = rowItem.getArrayList();
                questionRadioHolder.txtQuestion.setText(rowItem.getQuestionText());
                questionRadioHolder.rg.removeAllViews();
                questionRadioHolder.rg.clearCheck();
                for (int i = 0; i < arrayList.size(); i++) {
                    RadioButton radioButton = new RadioButton(getContext());
                    radioButton.setId(i);
                    radioButton.setTextSize(12);
                    radioButton.setText(arrayList.get(i));
                    radioButton.setTypeface(face1);
                    questionRadioHolder.rg.addView(radioButton);
                }

//                questionRadioHolder.rg.removeAllViews();
                questionRadioHolder.rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    public void onCheckedChanged(RadioGroup rg, int checkedId) {
                        for (int i = 0; i < rg.getChildCount(); i++) {
                            RadioButton btn = (RadioButton) rg.getChildAt(i);
                            ArrayList<String> list = new ArrayList<String>();
                            if (btn.getId() == checkedId) {
                                String text = btn.getText().toString();
                                int id = mList.get(position).getQuid();
                                System.out.println(position + "    position 2");
                                map.put(id, text);
                                list.add(0, text);
                                aMap1.put(id, list);
                                Log.d("radio", text);
                                Log.d("Queid:", "" + id);
                                return;
                            }
                        }
                    }
                });
            } else if (type == TYPE2) {
                if (convertView == null) {
                    convertView = mInflater.inflate(R.layout.checkbox_layout, parent, false);
                    questionCheckboxHolder = new QuestionCheckboxHolder();
                    questionCheckboxHolder.txtQuestionChk = (TextView) convertView.findViewById(R.id.chkQues);
                    questionCheckboxHolder.relativeLayout = (LinearLayout) convertView.findViewById(R.id.chkrl2);

                    Typeface face = Typeface.createFromAsset(getContext().getAssets(), "fonts/Raleway-SemiBold.ttf");
                    face1 = Typeface.createFromAsset(getContext().getAssets(), "fonts/Raleway-Regular.ttf");
                    questionCheckboxHolder.txtQuestionChk.setTypeface(face);

                    convertView.setTag(questionCheckboxHolder);
                } else {
                    questionCheckboxHolder = (QuestionCheckboxHolder) convertView.getTag();
                }


                List<String> arrayList = rowItem.getArrayList();
                questionCheckboxHolder.txtQuestionChk.setText(rowItem.getQuestionText());

                questionCheckboxHolder.relativeLayout.removeAllViews();

                for (int i = 0; i < arrayList.size(); i++) {
                    checkBox = new CheckBox(getContext());
                    checkBox.setId(i);
                    checkBox.setMinHeight(30);
                    checkBox.setMinimumWidth(30);
                    checkBox.setText(arrayList.get(i));
                    checkBox.setTypeface(face1);
                    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                            int position = (Integer) buttonView.getTag();


                            ArrayList<String> arraylist2 = new ArrayList<String>();
                            ArrayList<String> list = new ArrayList<String>();
                            int id = mList.get(position).getQuid();
                            arraylist2 = aMap1.get(id);
                            System.out.println(arraylist2);

                            if (arraylist2 == null) {
                                arraylist2 = list;
                            }
                            if (buttonView.isChecked()) {
                                arraylist2.add(buttonView.getText().toString());
                                aMap1.put(id, arraylist2);

                            } else {
                                arraylist2.remove(buttonView.getText().toString());
                                aMap1.put(id, arraylist2);
                            }
                            System.out.println(aMap1.get(id));

                        }
                    });




                    questionCheckboxHolder.relativeLayout.addView(checkBox);
                }
            }

            return convertView;
        }

如果我滚动列表视图,它会自动取消选中复选框,如何选中复选框。

【问题讨论】:

标签: android listview android-fragments android-listfragment


【解决方案1】:

试试这个对我有用的方法

public class CustomAdapter extends BaseAdapter {
    private final LayoutInflater inflater;
    private final Context context;
    private List<ModelCls> listData;

    public CustomAdapter(Context mainActivity, List<ModelCls> listData) {
        context = mainActivity;
        this.listData = listData;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.list_item_poojaselection, null);
            holder.tv = (TextView) convertView.findViewById(R.id.list_item_poojaname);
            holder.checks = (CheckBox) convertView.findViewById(R.id.list_item_poojacheck);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.checks.setOnCheckedChangeListener(null);
        holder.checks.setFocusable(false);

        if (listData.get(position).isselected) {
            holder.checks.setChecked(true);
        } else {
            holder.checks.setChecked(false);
        }

        holder.checks.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton cb, boolean b) {

                if (checkMaxLimit()) {

                    if (listData.get(position).isselected && b) {
                        holder.checks.setChecked(false);
                        listData.get(position).isselected = false;

                    } else {
                        holder.checks.setChecked(false);
                        listData.get(position).isselected = false;
                        Toast.makeText(context, "Max limit reached", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    if (b) {
                        listData.get(position).isselected = true;
                    } else {
                        listData.get(position).isselected = false;
                    }
                }
            }
        });

        holder.tv.setText(listData.get(position).getLISTING_NAME());
        return convertView;
    }

    public boolean checkMaxLimit() {
        int countermax = 0;
        for(ModelCls item : listData){
            if(item.isselected){
                countermax++;
            }
        }
        return countermax >= 5;
    }

    public class ViewHolder {
        TextView tv;
        public CheckBox checks;
    }
}

如果你想 recyclerview 试试这个

http://android-pratap.blogspot.in/2015/01/recyclerview-with-checkbox-example.html

【讨论】:

    【解决方案2】:

    将您的检查清单保存在ArrayList&lt;Boolean&gt;,并将其加载到getView()

    ArrayList<Boolean> listCheck;
    
    public YourAdapter(Context context, int resource, ArrayList<T> list) {
                super(context, resource, list);
        listCheck = new ArrayList<Boolean>;
        for (int i = 0; i < list.size(); i++) {
            listCheck.add(false); // init listCheck
        }
    } 
    
    public View getView(final int position, View convertView, ViewGroup parent) {
       yourCheckBox.setChecked(listCheck.get(position)); // set checkbox
       yourCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
               @Override
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
               listCheck.set(position, isChecked); // update listChecked
          }
       }
    }
    

    【讨论】:

    • 但我在 listview 行中有多个检查。怎么做?你能指导我吗
    • 你的意思是在每一行你有多个检查?您能描述一下您的需求或一些图片吗?
    【解决方案3】:

    如果你勾选了checkbox,你可以把它保存到HashMap(key:position, value:true/false),在getView()方法中你可以区分哪个是勾选的或者不勾选的,然后让复选框显示它的状态。

    【讨论】:

      【解决方案4】:

      尝试使用 model classarray 来保留 checkbox 的值并在 onclick 上更新它,如果 checkbox 可以解决您的问题我认为.

       holder.checkBox.setChecked(mCheckList.get(position));
                holder.checkBox.setOnClickListener(new View.OnClickListener() {
                @Override
                  public void onClick(View v) {
                      mCheckList.set(position, !mCheckList.get(position));
                      notifyDataSetChanged();
                   }
                });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-06-09
        • 1970-01-01
        • 1970-01-01
        • 2016-09-05
        • 1970-01-01
        • 2012-12-15
        • 1970-01-01
        相关资源
        最近更新 更多