0

I got a list from the server and set it to the adapter's RecyclerView.
So when I selecting on item 0 -> all items are selected/deselected that in this case, is working.

multi-selection recyclerView with checkBox

but But when I selected all items and when I deselected checkBox 1 expect deselected box 0.

Adapter class

 

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

 
 

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

public class VagozariSelectedAmlakAdapter extends RecyclerView.Adapter<VagozariSelectedAmlakAdapter.ViewHolder> {


    private Context context;
    private ArrayList<ClientsDataSet> listAmlak;
    boolean isCheckAll = false;
    ItemClickListener itemClickListener;
    int selectedPosition = -1;

    public static ArrayList<String> clientsList = new ArrayList<>();
    private RadioButton lastCheckedRB = null;



    public VagozariSelectedAmlakAdapter(Context context, ArrayList<ClientsDataSet> listAmlak, ItemClickListener itemClickListener) {
        this.context = (Activity) context;
        this.listAmlak = listAmlak;
        this.itemClickListener = itemClickListener;



    }

    @NonNull
    @Override
    public ViewHolder
    onCreateViewHolder(@NonNull ViewGroup parent,
                       int viewType) {
        // Initialize view
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_archive_selcted_amlak, parent,
                        false);
        // Pass holder view
        return new ViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull @NotNull VagozariSelectedAmlakAdapter.ViewHolder holder, int position) {



        if (listAmlak.get(position).selected == true) {
            holder.radioButton.setChecked(true);
        } else {
            holder.radioButton.setChecked(false);
        }



        if (listAmlak.get(position).name != null) {
            holder.name_amlak.setText(listAmlak.get(position).name);

        } else {
            holder.name_amlak.setVisibility(View.GONE);
            holder.radioButton.setVisibility(View.GONE);
        }


        holder.radioButton.setOnCheckedChangeListener(
                new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(
                            CompoundButton compoundButton,
                            boolean statet) {
                        // check condition
                        if (statet) {
                            if (position == 0) {
                                for (int i = 0; i < listAmlak.size(); i++) {
                                    ClientsDataSet dataSet = listAmlak.get(i);
                                    dataSet.selected = true;
                                    listAmlak.set(i, dataSet);

                                }
                                notifyDataSetChanged();

                            } else if (position != 0 && position > 0) {


//                                Toast.makeText(context,position + "" , Toast.LENGTH_LONG).show();
                                ClientsDataSet dataSet = listAmlak.get(position);
                                dataSet.selected = true;
                                listAmlak.set(position, dataSet);

                                ClientsDataSet alltfalse = listAmlak.get(0);
                                alltfalse.selected = false;
                                listAmlak.set(0, alltfalse);
////
                                 notifyDataSetChanged();
////                                clientsList.add(listAmlak.get(position).id);
                            }


                        } else {
                            if (position == 0) {
                                for (int i = 0; i < listAmlak.size(); i++) {
                                    ClientsDataSet dataSet = listAmlak.get(i);
                                    dataSet.selected = false;
                                    listAmlak.set(i, dataSet);
                                    isCheckAll = false;
                                }
                                notifyDataSetChanged();
                            } else if (position != 0 && position > 0) {


                                ClientsDataSet dataSet = listAmlak.get(position);
                                dataSet.selected = false;
                                listAmlak.set(position, dataSet);


                                ClientsDataSet alltfalse = listAmlak.get(0);
                                alltfalse.selected = false;
                                listAmlak.set(0, alltfalse);
//
                                notifyDataSetChanged();
                            }


                        }
                    }
                });




    }


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

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    // MY VIEW HOLDER CLASS ------------------------
    public class ViewHolder
            extends RecyclerView.ViewHolder {
        // Initialize variable
        CheckBox radioButton;
        TextView name_amlak;
        ConstraintLayout root;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            // Assign variable
            radioButton = itemView.findViewById(R.id.item_vagozary_radioButton_selceting_amlak);
            name_amlak = itemView.findViewById(R.id.item_vagozary_name_amlak);
            root = itemView.findViewById(R.id.root);
        }
    }


    public interface ItemClickListener {
        // Create method
        void onClick(String s);
    }


}

**call adapter in Main **

LinearLayoutManager layoutManager = new LinearLayoutManager(SelectedAmlakActivityV2, LinearLayoutManager.VERTICAL, false);
lists.setLayoutManager(layoutManager);
ClientsDataSet item = new ClientsDataSet();
item.name = "all";
apiClients.clients.add(0, item);


adapter = new VagozariSelectedAmlakAdapter(SelectedAmlakActivityV2, apiClients.clients, itemClickListener);
lists.setAdapter(adapter);

Error log

 java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling androidx.recyclerview.widget.RecyclerView{6631a12 VFED..... ......ID 0,0-1048,1400 #7f090731 app:id/vagozariSelectedAmlak}, adapter:com.adapters.VagozariSelectedAmlakAdapter@60fc5e3, layout:androidx.recyclerview.widget.LinearLayoutManager@3949be0, context:com.view.activity.vagozariMelk.makeVagozary.SelectedAmlakByUserActivity@3b4a0d1
        at androidx.recyclerview.widget.RecyclerView.assertNotInLayoutOrScroll(RecyclerView.java:3051)
        at androidx.recyclerview.widget.RecyclerView$RecyclerViewDataObserver.onChanged(RecyclerView.java:5536)
        at androidx.recyclerview.widget.RecyclerView$AdapterDataObservable.notifyChanged(RecyclerView.java:12253)
        at androidx.recyclerview.widget.RecyclerView$Adapter.notifyDataSetChanged(RecyclerView.java:7354)
        at com.VagozariSelectedAmlakAdapter$1.onCheckedChanged(VagozariSelectedAmlakAdapter.java:114)
        at android.widget.CompoundButton.setChecked(CompoundButton.java:180)
        at com.app.adapters.VagozariSelectedAmlakAdapter.onBindViewHolder(VagozariSelectedAmlakAdapter.java:69)
        at com.app.adapters.VagozariSelectedAmlakAdapter.onBindViewHolder(VagozariSelectedAmlakAdapter.java:26)
        at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
        at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
        at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
        at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
        at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
        at android.view.View.layout(View.java:21912)
        at android.view.ViewGroup.layout(ViewGroup.java:6260)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
        at android.view.View.layout(View.java:21912)
        at android.view.ViewGroup.layout(ViewGroup.java:6260)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
        at android.view.View.layout(View.java:21912)
        at android.view.ViewGroup.layout(ViewGroup.java:6260)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:21912)
        at android.view.ViewGroup.layout(ViewGroup.java:6260)
        at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1873)
        at android.view.View.layout(View.java:21912)
        at android.view.ViewGroup.layout(ViewGroup.java:6260)
        at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1873)
        at android.view.View.layout(View.java:21912)
        at android.view.ViewGroup.layout(ViewGroup.java:6260)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
   

 

My brief work
#1: Box 0 is selected (so all items in the list are selected)
#2: When I deselect boxes 1, 2, or 3, I want to deselect box 0, which results in a crash (in #1).
And conversely

Amin
  • 342
  • 4
  • 15
  • Can you please add the crash log too. – Kozmotronik Dec 13 '22 at 07:45
  • @Kozmotronik - I added the result of the crash in the question, so if you have a good solution for my solution, please share – Amin Dec 13 '22 at 09:34
  • Have you looked at [this](https://stackoverflow.com/questions/43221847/cannot-call-this-method-while-recyclerview-is-computing-a-layout-or-scrolling-wh), [this](https://stackoverflow.com/questions/27070220/android-recyclerview-notifydatasetchanged-illegalstateexception) and [this](https://stackoverflow.com/questions/42944005/recyclerview-cannot-call-this-method-in-a-scroll-callback)? Those can help you with the crash issue since they are very similar to your issue. Let me know if you have further problems with your app logic which is not API or crash related. – Kozmotronik Dec 13 '22 at 10:13
  • 1
    In your question you specify: *#2: When I deselect boxes 1, 2, or 3, I want to deselect box 0, which results in a crash (in #1)*. That's why first of all you should get rid of this crash. According to the answers which I shared in my previous comment, you should either move your viewholder-related code implementation into its viewholder class or use post method in order to execute the code synchronized to the recyclerview's next frame. Do you mind fixing your code or you just keep going with the existing buggy one and fix logical issues on it? – Kozmotronik Dec 13 '22 at 11:00
  • @Kozmotronik : I FOUND that My main Crash happens at some point when using notifyDataSetChanged() on "if else" – Amin Dec 13 '22 at 12:28
  • Dear @Amin, Please have look the referenced answers and try to move the viewholder's UI logic into the viewholder class. This is some kind of design implementation mistake. Then it will be easier to achive to the way you want on how the check and uncheck work. – Kozmotronik Dec 13 '22 at 12:58
  • Try use OnClickedListener instead of OnCheckedChangeListener. – i_A_mok Dec 14 '22 at 10:56
  • @Kozmotronik Thanks for your response, finally I solved this issue. I used a local Database and set data to DB from the server. I did use Database Sugar ORM too easily. I wanted to solve the issue without using a local DB, but I had to use a local DB and it become too easy. – Amin Apr 24 '23 at 09:42

1 Answers1

1

Try adding holder.radioButton.setOnCheckedChangeListener(null); as the very first line of your onBindViewHolder() method.

HANiS
  • 530
  • 5
  • 9