Data List duplicate after go to other fragment (Sliding Tab)
Asked Answered
P

2

6

there are 3 tabs : ONE || TWO || TRI

example : myList = 1,2,3

issue :

after i go to page TWO myList = 1,2,3,1,2,3 (double duplicate)

if i go to page TRI myList = 1,2,3,1,2,3,1,2,3 (triple duplicate)

after i looking for the solution on internet, i found this code (on Adapter class) :

   public void swap(List<FoodModel> datas){
        datas = new ArrayList<>();//updated
        if(mListFood !=null || mListFood.size() !=0){
            mListFood.clear();
            mListFood.addAll(datas);
        }else{
            mListFood = datas;
        }
        notifyDataSetChanged();
    }

i used like this in oneFragment.java:

   mListFoodAdapter = new ListFoodAdapter(getContext(), mFoodModel);
        mListFoodAdapter.swap(mFoodModel);
        mRecyclerViewListFood.setLayoutManager(linearLayoutManager);
        mRecyclerViewListFood.setAdapter(mListFoodAdapter);
        mRecyclerViewListFood.setItemAnimator(new DefaultItemAnimator());

but it gave me nullPointer sometimes there is no data show in RecyclerView

please, suggest me how to make the list data isn't duplicate anymore after back from page TWO of TRI

bellow my codes oneFragment.java and ViewPagerAdapter.java

oneFragment.java

public class FoodFragment extends Fragment {

    private RecyclerView mRecyclerViewListFood;
    private List<FoodModel> mFoodModel = new ArrayList<>();
    private ListFoodAdapter mListFoodAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_foods, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        FragmentActivity fragmentActivity = getActivity();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(fragmentActivity);
        mRecyclerViewListFood = (RecyclerView) view.findViewById(R.id.recyclerView_list_foods);
        mFoodModel.add(new FoodModel("1",String.valueOf(R.drawable.icon), "test 1"));
        mFoodModel.add(new FoodModel("2",String.valueOf(R.drawable.icon), "test 2"));

        mListFoodAdapter = new ListFoodAdapter(getContext(), mFoodModel);
        mRecyclerViewListFood.setLayoutManager(linearLayoutManager);
        mRecyclerViewListFood.setAdapter(mListFoodAdapter);
        mRecyclerViewListFood.setItemAnimator(new DefaultItemAnimator());



    }
}

ViewPagerAdapter.java :

public class ViewPagerDetailStandAdapter extends FragmentStatePagerAdapter {

    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerDetailStandAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}
Pyelonephritis answered 16/1, 2016 at 10:13 Comment(2)
Yes post your adapter and fragment codeLaser
@chandil03 i have edited my codes, please review my codes.Pyelonephritis
R
5

just use this method to clear your fragment data.

@Override
public void onDestroyView() {
    super.onDestroyView();
    tutPojo.clear();
}
Running answered 11/7, 2017 at 13:25 Comment(0)
L
4

There is a problem in you onViewCreated() method. OnViewCreated() method is called each time after fragment's view is created. But fragment is reused if it is in memory. That is the reason that your mFoodModel list doesn't get reinitialized. So you need to reinitialize mFoodModel to create previous data.

So this is your modified OnCreateView()`

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    FragmentActivity fragmentActivity = getActivity();
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(fragmentActivity);
    mRecyclerViewListFood = (RecyclerView) view.findViewById(R.id.recyclerView_list_foods);
    mFoodModel = new ArrayList<>(); // initialize you list
    mFoodModel.add(new FoodModel("1",String.valueOf(R.drawable.icon), "test 1"));
    mFoodModel.add(new FoodModel("2",String.valueOf(R.drawable.icon), "test 2"));

    mListFoodAdapter = new ListFoodAdapter(getContext(), mFoodModel);
    mRecyclerViewListFood.setLayoutManager(linearLayoutManager);
    mRecyclerViewListFood.setAdapter(mListFoodAdapter);
    mRecyclerViewListFood.setItemAnimator(new DefaultItemAnimator());
}
Laser answered 17/1, 2016 at 9:19 Comment(2)
thanks @chandil03, i have used ` mFoodModel = new ArrayList<>();` in my adapter (in code that i said as solution, cek updated questions) what make them different?Pyelonephritis
OnViewCreated() method is called each time fragment is loaded where you are adding items. again and again you load fragment your code will add same items in the list. Because fragment was reused hence your initialization statement of mFoodModel list gets initialized only once. You need to reinitialize your list again or clear data from list in OnViewCreated method to solve your problem.Laser

© 2022 - 2024 — McMap. All rights reserved.