MeknessiHamida
MeknessiHamida

Reputation: 185

RecyclerView List not correct displayed ArrayList

I am populating my recyclerView with an arrayList depending on selected item and rest service call. The problem is that at first for the default selected item it gets correctly populated but the moment I choose another item to get its data, things get weird : it gets the list as if I kept the previous item selected, it dosen't get synchronized, I checked that the choice is being passed to the rest webservice to bring back the correct data. Any ideas ?

Here is my code :

       facturelist=new ArrayList<>();
   Toast.makeText(getActivity().getApplicationContext(),"fac Ond"+ondSel, Toast.LENGTH_SHORT).show();
    apiService = RestService.createService(SolarAPIService.class);
    Call<List<ListFacture>> call = apiService.listFactureStringDate(ondSel);

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

            for (ListFacture fac : response.body()) {
                facturelist.add(fac);
                factureAdapter.notifyDataSetChanged();

            }
            Collections.sort(facturelist, new Comparator<ListFacture>() {
                public int compare(ListFacture o1, ListFacture o2) {
                    if (o1.getDateDebut() == null || o2.getDateDebut() == null)
                        return 0;
                    return o2.getDateDebut().compareTo(o1.getDateDebut());
                }
            });

            Toast.makeText(getActivity().getApplicationContext(),"facture List"+facturelist.size(), Toast.LENGTH_SHORT).show();
            factureAdapter.notifyDataSetChanged();
            recyclerView.setAdapter(factureAdapter);
            recyclerLay.setVisibility(View.GONE);
            slideDown= AnimationUtils.loadAnimation(getActivity().getApplicationContext(),R.anim.slide_up);
            recyclerLay.startAnimation(slideDown);
            recyclerLay.setVisibility(View.VISIBLE);
            progress.setVisibility(View.GONE);
        }catch (NegativeArraySizeException nx){
            progress.setVisibility(View.GONE);
            layoutFactures.setVisibility(View.GONE);
            notFound.setVisibility(View.VISIBLE);
            errorNotFound.setText(R.string.onduleur_alerts_404);
        }
        }

        @Override
        public void onFailure(Call<List<ListFacture>> call, Throwable t) {
       Log.e("Error",t.getMessage());
            Toast.makeText(getActivity().getApplicationContext(),"facture List"+t.getStackTrace(), Toast.LENGTH_SHORT).show();
        }
    });

}

It looks as if factureList keeping its previous state.

Upvotes: 0

Views: 46

Answers (1)

Josef Adamcik
Josef Adamcik

Reputation: 5780

You are creating new instance for fractureList before new request but you are not changing instance (or its content) in your adapter.

You should change list in your Adapter to the new instance or recreate the adapter from scratch.

Upvotes: 1

Related Questions