7

I am trying to remove an item from my RecyclerView, but I always get an error.

java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling

I am using notifyDataSetChanged(). How can I solve this?

Here is my adapter code

public class ListAdapters extends RecyclerView.Adapter<ListAdapters.MyViewHolder> {

    public ArrayList<String> tvdatalist;
    Context c;
    int pos;
    ListAdapters.MyViewHolder myViewHolder;
    private LayoutInflater layoutInflater;
    int[] arr;

    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {


        public EditText edttxt;
        public CheckBox cb;

        public MyViewHolder(View view) {
            super(view);

            edttxt = (EditText) view.findViewById(R.id.edttxt);
            cb = (CheckBox) view.findViewById(R.id.cb);
        }

        @Override
        public void onClick(View v) {

        }


    }

    public ListAdapters(Context c, ArrayList<String> tvdatalist) {
        this.c = c;
        this.layoutInflater = LayoutInflater.from(c);
        this.tvdatalist = tvdatalist;
        arr = new int[tvdatalist.size()];
        for (int i = 0; i < 20; i++) {
            arr[i] = 0;

        }


    }

    @Override
    public ListAdapters.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ListAdapters.MyViewHolder(this.layoutInflater.inflate(R.layout.list_ittm, parent, false));
        //return new LandingPageAdapter.MyViewHolder(itemView);
    }

    public void onBindViewHolder(final ListAdapters.MyViewHolder holder, final int position) {
        myViewHolder = holder;

        final String ShowsBean = tvdatalist.get(position);
        myViewHolder.edttxt.setText(ShowsBean);
        if (arr[pos] == 0) {
            myViewHolder.cb.setChecked(false);
            myViewHolder.edttxt.setKeyListener(null);
        } else {
            myViewHolder.cb.setChecked(true);
            myViewHolder.edttxt.setFocusable(true);
        }



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

                    if (arr[position]==0){
                        arr[position]=1;
                    }else{
                        arr[position]=0;

                    }
                 notifyDataSetChanged();

            }

        });

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

    }

    public interface ItemClickListener {
        void onClick(View view, int position, boolean isLongClick);
    }
}
3
  • 3
    This usually occurs when you are calling notify on bg thread. So just move notify to ui thread. Commented Jul 26, 2017 at 10:54
  • 1
    couldn't get you. can you please describe it? Commented Jul 26, 2017 at 10:57
  • 1
    Really can't think of another way to describe it. You wherever you are calling notifyDataSetChanged()(seems to be on background thread) from move it a UI thread. If you don't know the difference between background thread and UI thread then do some research. Commented Jul 26, 2017 at 11:02

3 Answers 3

15

As Abbas said

This usually occurs when you are calling notify on bg thread. So just move notify to ui thread

else you can use notifyItemChanged(position);

this will also worked as well.

Sign up to request clarification or add additional context in comments.

1 Comment

i also use this in onbinholder class but still app crash and give this error
4

You can also use runnable to make changes on UI thread.

recyclerView.post(new Runnable() {
    @Override
    public void run() {
        notifyDataSetChanged();
    }
});

This will notify adapter changes on UI thread.

Comments

0

This problem shows when you are trying to update the recyclerview when it still inflating the item, try to move the notifyItemChanged(position), to where you are sure the item has been added to your view before calling the- method(notifyItemChanged(position));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.