11

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

R. Zagórski
  • 20,020
  • 5
  • 65
  • 90
Tiseno
  • 111
  • 1
  • 1
  • 4
  • Please format your question - insert 4 spaces on each line to get it to appear as code – Tim Mar 28 '16 at 08:48
  • 5
    Why someone downvote my question. sad life.. – Tiseno Mar 28 '16 at 08:53
  • Not me my friend, I was just working on the review queue, which doesn't vote. It's good practice to describe the issue that they see when down voting, sorry to see it not done here. – Tim Mar 28 '16 at 08:55
  • Removed unnecessary space, so that the code will be properly indent and will be helpful for others to go through it and answer it. – Prafulla Kumar Sahu Mar 30 '16 at 08:58

3 Answers3

36

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.

itzmebibin
  • 9,199
  • 8
  • 48
  • 62
marmor
  • 27,641
  • 11
  • 107
  • 150
5

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();
    
Community
  • 1
  • 1
Sunil
  • 3,785
  • 1
  • 32
  • 43
0

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;
}
Nermeen
  • 15,883
  • 5
  • 59
  • 72