Home » Android » android – RecyclerView java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position when resume from added fragment

android – RecyclerView java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position when resume from added fragment

Posted by: admin May 14, 2020 Leave a comment

Questions:

Can you guys help me on this

here is my code to add fragment

mRecyclerView.addOnItemTouchListener(
    new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            if (!IsScrolling) {
                Fragment fragment = new ProductInfoFragment();
                    Bundle bundle = new Bundle();
                    bundle.putString("prodID", mItems.get(position).getproductID());
                    bundle.putString("catName", catName);
                    fragment.setArguments(bundle);
                    FragmentManager fragmentManager = getActivity().getFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                                        fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                                        fragmentTransaction.add(R.id.fragmentContainer, fragment, "ProuctInfoFragment");
                                        fragmentTransaction.addToBackStack(null);
                                        fragmentTransaction.commit();
                                        mItems.clear();
                                    }

                                }

                            })
                    );

But when i press back from the added fragment i got this error and my app crash.

FATAL EXCEPTION: main Process: com.tiseno.poplook, PID: 2617
 java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 5(offset:5).state:9
      at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4401)
      at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4359)
      at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)
      at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:438)
      at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333)
      at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1161)
      at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1018)
      at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:3807)
      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
      at android.view.Choreographer.doCallbacks(Choreographer.java:670)
      at android.view.Choreographer.doFrame(Choreographer.java:603)
      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
      at android.os.Handler.handleCallback(Handler.java:739)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.os.Looper.loop(Looper.java:224)
      at android.app.ActivityThread.main(ActivityThread.java:5514)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

why is this happening? please help. im using two fragment. the product list fragent and product details fragment

How to&Answers:

you’re calling mItems.clear(); without notifying the RecyclerView‘s adapter that something in the data had changed.
after each change to the data used by the adapter you need to call adapter.notifyDataSetChanged();

See this.

Answer:

Try this

i was facing the same issue ,when you are clearing your list you have to notify the adapter because everytime you are changing the list and the adapter is containing old list so you have to notify the adapter after clearing the list before adding new items in adapter

  1. make instance of data members in starting oof your class

    ArrayList<HashMap<String,String>> yourarraylist =new ArrayList<>();
    Youradaptername instance;
    
  2. after clearing your list notify the adapter

     yourarraylist .clear();
     instance.notifyDataSetChanged();
    

Answer:

The issue may be related to the adapter. If you are using setHasStableIds(true); in the adapter constructor, you need to make sure that you are using stable ids.

Make sure that you override getItemId correctly in the adapter.
It should be:

@Override
public long getItemId(int position) {
   return yourList == null ? 0 : yourList.get(position).getId();
}

And not:

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