Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
189 views
in Technique[技术] by (71.8m points)

java - data is duplicating when navigate back to previous fragment

I am using navigation component in my app I have 2 fragments one fragments list of items and another shows detail of an item when user clicks on an item in fragments 1 it goes to detail fragment and when I switch back to first fragment then all the listing duplicates again.Below is my code:

CakeFragment.java

public class CakeFragment extends Fragment {

List<AllCakes> allCakeList = new ArrayList<>();
AllCakesAdapter adapter;
BottomNavigationView  navView;
FragmentCakeBinding fragmentCakeBinding;

public CakeFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    fragmentCakeBinding = FragmentCakeBinding.inflate(inflater,container,false);

    navView = getActivity().findViewById(R.id.navView);
    navView.setVisibility(View.GONE);

    getAllCakes();

    return fragmentCakeBinding.getRoot();
}

private void getAllCakes(){

    Retrofit retrofit = RetrofitClient.getInstance();
    ApiService apiService  = retrofit.create(ApiService.class);

    Call<List<AllCakes>> call = apiService.getAllCake();

    call.enqueue(new Callback<List<AllCakes>>() {
        @Override
        public void onResponse(Call<List<AllCakes>> call, Response<List<AllCakes>> response) {

            fragmentCakeBinding.cakeProgress.setVisibility(View.INVISIBLE);

            fragmentCakeBinding.allCakeRecycler.setHasFixedSize(true);
            fragmentCakeBinding.allCakeRecycler.setLayoutManager(new LinearLayoutManager(getActivity()));
            allCakeList.addAll(response.body());
            adapter = new AllCakesAdapter(getActivity(),allCakeList);
            fragmentCakeBinding.allCakeRecycler.setAdapter(adapter);
        }

        @Override
        public void onFailure(Call<List<AllCakes>> call, Throwable t) {

            fragmentCakeBinding.cakeProgress.setVisibility(View.INVISIBLE);
            TastyToast.makeText(getActivity(),t.getMessage(),TastyToast.LENGTH_SHORT,TastyToast.ERROR).show();
        }
    });
  }
}

AllCakesAdapter.java

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

Context context;
List<AllCakes> allCakeList;

public AllCakesAdapter(Context context, List<AllCakes> allCakeList) {
    this.context = context;
    this.allCakeList = allCakeList;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.allcakes_row,parent,false);
    return new ViewHolder(v);
}

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

    AllCakes model = allCakeList.get(position);
    Glide.with(context).load(model.getImgurl()).into(holder.allCakeImg);
    holder.allCakeName.setText(model.getName());

    holder.cakeDisPrice.setPaintFlags(holder.cakeDisPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

    holder.moreCake.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            NavController navController = Navigation.findNavController((Activity) context,R.id.fragment);
            navController.navigate(R.id.cakeDetailFragment);
        }
    });
}

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

public class ViewHolder extends RecyclerView.ViewHolder {

    ImageView allCakeImg;
    Button moreCake;
    TextView allCakeName;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        
        allCakeImg = itemView.findViewById(R.id.allCakeImg);
        moreCake = itemView.findViewById(R.id.moreCake);
        allCakeName = itemView.findViewById(R.id.allCakeName);
      }
   }
}

CakeDetailFragment.java

public class CakeDetailFragment extends Fragment {

FragmentCakeDetailBinding fragmentCakeDetailBinding;

public CakeDetailFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    fragmentCakeDetailBinding = FragmentCakeDetailBinding.inflate(inflater,container,false);



    return fragmentCakeDetailBinding.getRoot();
  }
} 

Someone please let me know why its happening. Any help would be appreciated.

THANKS

question from:https://stackoverflow.com/questions/65937140/data-is-duplicating-when-navigate-back-to-previous-fragment

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
private void getAllCakes(){
            ...
            allCakeList.addAll(response.body());
            adapter = new AllCakesAdapter(getActivity(),allCakeList);
            fragmentCakeBinding.allCakeRecycler.setAdapter(adapter);
        }
            ...

you're calling: allCakeList.addAll(response.body());

every time without clearing your list out.

you have to clear that list:

allCakeList.clear();
allCakeList.addAll(response.body());

this is something you can easily determine yourself by just putting a breakpoint on your allCakeList to see what's inside it, if you haven't ever done this before, you should consider trying it out


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...