Skip to content Skip to sidebar Skip to footer

How To Solve Duplicate Data Items In Recyclerview

I get duplicate data items in the recyclerview after the location data has been updated I have tried some such like way NotifyDataSetChanged () and setHasStableIds (true) But still

Solution 1:

Add below method in your FragmentPetaniTerdekat to check the duplicate entry:

public boolean isExist(String strNama) {

    for (int i = 0; i < nama.size(); i++) {
        if (nama.get(i).equals(strNama)) {
            returntrue;
        }
    }

    returnfalse;
}

Use this method inside onResponse(), before adding string into lists(nama, jarak, durasi):

@OverridepublicvoidonResponse(String response) {
            if (response != null) {
                try {

                    ..............
                    ..................   

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

                        JSONObject result = get_result.getJSONObject(i);

                        ModelPetaniTerdekat petaniTerdekat = newModelPetaniTerdekat();
                        petaniTerdekat.setNama(result.getString("nama"));

                        JSONObject kriteria = result.getJSONObject("jarak");
                        for (int z=0; z<kriteria.length(); z++){
                            petaniTerdekat.setJarak(kriteria.getString("distance"));
                            petaniTerdekat.setDurasi(kriteria.getString("duration"));
                        }

                        boolean isExist = isExist(petaniTerdekat.getNama());

                        if (!isExist) { // Not exist, Add now
                            nama.add(petaniTerdekat.getNama());
                            jarak.add(petaniTerdekat.getJarak());
                            durasi.add(petaniTerdekat.getDurasi());
                        }

                        dialog.closeDialog();
                    }

                    initRecylerView(view);

                } catch (JSONException e) {
                    dialog.message("Error : "+e.toString());
                    e.printStackTrace();
                }
            }else{
                dialog.message("Error : Tidak ada data !");
            }
        }

Hope this will help~

Post a Comment for "How To Solve Duplicate Data Items In Recyclerview"