user8164155
user8164155

Reputation: 127

How to remove duplicates from ArrayList<HashMap<String,String>>()

in my retrofit service response i've using ArrayList<HashMap<String,String>>() and HashMap<String, String>() to display some values.when displaying data,it's duplicated. also i'm clear list list.clear() befor calling the function.

ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();

HashMap<String,String> hashmap = new HashMap<String, String>();



    public void displayList(){
            list.clear();

            WebserviceAPI apiService =retrofit.create(WebserviceAPI.class);
            Call<OrderDetailsResponse> call = apiService.displayOrder("orderdetails",token_acces,id_order);
            call.enqueue(new Callback<OrderDetailsResponse>() {
                @Override
                public void onResponse(Call<OrderDetailsResponse> call, Response<OrderDetailsResponse> response) {

                        OrderDetailsResponse result = response.body();
                        List<OrderDetails> data=result.getData();
                        returnstatus=result.isStatus();
                        msg= result.getMessage();

                        if(returnstatus){
                            for (OrderDetails a: data){

                                hashmap.put(FIRST_COLUMN, a.getProduct_name());
                                hashmap.put(SECOND_COLUMN, a.getProduct_quantity());
                                hashmap.put(THIRD_COLUMN, a.getOriginal_product_price());

                                list.add(hashmap);
                                Log.d("ressorder",""+list);


                            }
                                adapter=new ListViewAdapter(getActivity(), list);
                                listView.setAdapter(adapter);

                        }
                }

                @Override
                public void onFailure(Call<OrderDetailsResponse> call, Throwable t) {
                    Log.d("fragerr",""+t.getMessage());
                    progress.dismiss();

                }
            });

        }

my log cat

[{Second=1, First=Pen, Third=27.00}, {Second=1, First=Pen, Third=27.00}, {Second=1, First=Book, Third=15.00}, {Second=1, First=Book, Third=15.00}, {Second=1, First=Pencil, Third=12.00}, {Second=1, First=Pencil, Third=12.00}]

it's repeated same twise

Upvotes: 0

Views: 208

Answers (1)

algrid
algrid

Reputation: 5954

You seem to add the same hashmap to your list multiple times. Try to create a new hashmap on each iteration of the loop calling hashmap = new HashMap<String, String>()

Upvotes: 1

Related Questions