Android RecyclerView复选框会自行检查 [英] Android RecyclerView checkbox checks itself

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

问题描述

我有一个带有复选框和textview的RecyclerView.数字10、20、30、40 ...直到500都应显示在textview中.Checked复选框应在与该复选框相对应的textview中添加数字. .如果用户仅检查值10,则textView将显示10.如果用户也检查20,则 TextView将显示30(20 +10). 如果用户再次取消选中10,则TextView将显示20,依此类推.当我单击复选框时,也会选中一些随机复选框.我尝试了stackoverflow中的一种解决方案.它没有用.我对此感到困惑.请帮助我.这是我的代码:

I have a RecyclerView which has a checkbox and textview.Numbers 10,20,30,40... till 500 should be shown in textview.The Checked checkboxes should add the numbers in the textview corresponding to the checkbox.For eg. if User checks the value 10 only, the textView would show 10. If user checks 20 as well, then TextView would show 30 ( 20 +10). If user uncheck 10 again, the TextView would show 20, and so on.When i click on checkbox some random checkbox is also checked.I tried one solution in stackoverflow. It did not work.I am stuck with this.Please help me..Here is my code:

RecyclerAdapter.java:

RecyclerAdapter.java:

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerHolder>
    {

        @NonNull
        @Override
        public RecyclerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new RecyclerHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_lyt,parent,false));
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerHolder holder, final int position) {
            holder.number.setText(Integer.toString((Integer) alldata.get(position)));
            final String text=holder.number.getText().toString();


            holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    Log.e("Checked","Checked");
                    if(checkeddata!=null)
                    {
                        if (checkeddata.size()==0)
                        {
                            checkeddata.add(text);

                        }
                        else {
                            if(checkeddata.contains(text))
                            {
                                checkeddata.remove(text);


                            }
                            else {
                                checkeddata.add(text);

                        }
                        }
                        Iterator iterator=checkeddata.iterator();
                        int sumnumber=0;
                        while (iterator.hasNext())
                        {
                            sumnumber= sumnumber+Integer.parseInt((String) iterator.next());
                        }
                        sum.setText(Integer.toString(sumnumber));
                    }


                }
            });


        }

        @Override
        public int getItemCount() {
            return alldata.size();
        }

        public class RecyclerHolder extends RecyclerView.ViewHolder
        {
            TextView number;
            CheckBox checkBox;
            public RecyclerHolder(View itemView) {
                super(itemView);
                number=itemView.findViewById(R.id.number);
                checkBox=itemView.findViewById(R.id.check);


            }
        }
    }
    public void data()
    {
        int g=1;
        for(int i=10;i<=500;i++)
        {
            if((i/10)==g)
            {
                g=g+1;
                alldata.add(i);
            }

        }
    }

recycler_lyt.xml:

recycler_lyt.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:id="@+id/number"
    android:textSize="20sp"/>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/check"
        android:checked="false"
        app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>

推荐答案

这很正常.您没有选择复选框.您正在选择一个,并且视图持有者将其保持选中状态

It's normal. You are not setting your checkbox selected or not. You are selecting one and View holder keeps it selected

您可以看一下我的例子.您可以执行以下操作:

You may look at my example. You can do something like that:

@Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        final Data data = myItems.get(position);

        //in some cases, it will prevent unwanted situations
        holder.checkBox.setOnCheckedChangeListener(null);

        //if true, your checkbox will be selected, else unselected
        holder.checkBox.setChecked(data.isSelected());

        holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    //set your object's last status
                    data.setSelected(isChecked);
            }
        });

    }

onBindViewHolder内的

,您必须选中或未选中复选框. 如果为true,则会选中您的复选框,否则未选中

inside your onBindViewHolder you have to set your checkbox either checked or unchecked. if true, your checkbox will be selected, else unselected

holder.checkBox.setChecked(boolean);

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

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