0

I am populating a recycler view from a remote database.

The recyclerview is inside a fragment.

The first time the fragment is loaded, the recyclerview shows all items.

When the user clicks on an item, another fragment is loaded.

Then, if the user clicks on the back button, the recyclerview shows all items but now two times. After the last item all items are shown again.

I have included this two lines on the function that loads the data from the remote database:

    marcas.clear();// public List<Marca> marcas;
    adapter.notifyDataSetChanged();// private MarcaAdapter adapter;

But am I doing wrong?

EDIT

In OnCreate

   /**
         * Showing Swipe Refresh animation on activity create
         * As animation won't start on onCreate, post runnable is used
         */
        mSwipeRefreshLayout.post(new Runnable() {

            @Override
            public void run() {

                mSwipeRefreshLayout.setRefreshing(true);
                marcas.clear();
                adapter.notifyDataSetChanged();


            }
        });

        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);

        marcas = new ArrayList<>();


        gridLayout = new GridLayoutManager(getActivity(), 2);
        recyclerView.setLayoutManager(gridLayout);

        adapter = new MarcaAdapter(getActivity(), marcas);


        recyclerView.setAdapter(adapter);
      //  marcas.clear();



        getDirectoriosFromDB(0);//LLAMADA 1

To load the data:

private void getDirectoriosFromDB(int id) {

 marcas.clear();
 adapter.notifyDataSetChanged();

    AsyncTask<Integer, Void, Void> asyncTask = new AsyncTask<Integer, Void, Void>() {
        @Override
        protected Void doInBackground(Integer... addressesIds) {
          //  Log.d("HOLA PERFIL", "UID REGISTRADO ANTES DE CARGAR REECYCLER: " + user_id);



            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url("https://...marcas_todas.php")
                    .build();
            try {
                Log.d("HOLA ADDRESSES", "DIRECCION LEIDA:cargando datos en getdirectorios INICIO ARRAY" );
                okhttp3.Response response = client.newCall(request).execute();

                JSONArray array = new JSONArray(response.body().string());

                for (int i = 0; i < array.length(); i++) {

                    JSONObject object = array.getJSONObject(i);

                    Log.d("HOLA ADDRESSES", "DIRECCION LEIDA:cargando datos en getdirectorios " +array);
                    Log.d("HOLA ADDRESSES", "DIRECCION LEIDA: " + i);
                    Marca marca = new Marca(object.getInt("id_car_make"),
                            object.getString("name")

                            );

                    marcas.add(marca);
                }


            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {


            adapter.notifyDataSetChanged();
            mSwipeRefreshLayout.setRefreshing(false);
        }
    };

    asyncTask.execute(id);
}
mvasco
  • 4,965
  • 7
  • 59
  • 120
  • can you show your fragment or adapter code ? – Sandeep Malik Apr 18 '19 at 09:33
  • My best guess is that the `adapter` is losing the reference to the list `marcas` – Santanu Sur Apr 18 '19 at 09:33
  • @SandeepMalik, all fragment code? No problem – mvasco Apr 18 '19 at 09:34
  • where you are setting your adapter only that part of your adapter – Sandeep Malik Apr 18 '19 at 09:35
  • @SandeepMalik, ok – mvasco Apr 18 '19 at 09:36
  • Possible duplicate of [notifyDataSetChange not working from custom adapter](https://stackoverflow.com/questions/15422120/notifydatasetchange-not-working-from-custom-adapter) – Santanu Sur Apr 18 '19 at 09:38
  • @SandeepMalik, code added – mvasco Apr 18 '19 at 09:41
  • @SantanuSur, I don´t agree that my issue is the same as your duplicated question proposal, then you have marked my question as duplicated before I have put my code. Now, after having included my code, please tell me if it can be considered as duplicated. – mvasco Apr 18 '19 at 09:44
  • basically your list is global inside of activity but not in adapter so when u refresh list that time adapter have filled arraylist instance so this is creating issue. you should refill adapter after refreshing list : like this : mSwipeRefreshLayout.post(new Runnable() { @Override public void run() { mSwipeRefreshLayout.setRefreshing(true); marcas.clear(); adapter = new MarcaAdapter(getActivity(), marcas); adapter.notifyDataSetChanged(); } }); – unzila Apr 18 '19 at 10:16

1 Answers1

0

basically your list is global inside of activity but not in adapter so when u refresh list that time adapter have filled arraylist instance so this is creating issue. you should refill adapter after refreshing list : like this :

mSwipeRefreshLayout.post(new Runnable() {

        @Override
        public void run() {

            mSwipeRefreshLayout.setRefreshing(true);
            marcas.clear();
            adapter = new MarcaAdapter(getActivity(), marcas);
            adapter.notifyDataSetChanged();


        }
    });
unzila
  • 190
  • 1
  • 12
  • i have implemented your code, but the issue is there, all items repeated again – mvasco Apr 18 '19 at 10:40
  • tell me one thing when you are refreshing data your asynctask start again??? when you fetching data that time u need to clear list and then add data in your list – unzila Apr 18 '19 at 10:43
  • and you are calling notifydatasetchanged before starting asynctask in your getDirectoriosFromDB fucntion too, comment this line too – unzila Apr 18 '19 at 10:52