如何从ArrayList中删除重复项<hashmap <string,string>&gt;()

时间:2018-01-06 12:40:23

标签: android arraylist hashmap

在我的改造服务响应中,我使用ArrayList<HashMap<String,String>>()HashMap<String, String>()来显示一些值。当显示数据时,它是重复的。 我也清楚列出list.clear()来调用这个函数。

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();

                }
            });

        }

我的日志猫

[{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}]

重复同样的事情

1 个答案:

答案 0 :(得分:1)

您似乎多次将相同的hashmap添加到列表中。尝试在调用hashmap = new HashMap<String, String>()

的循环的每次迭代中创建一个新的hashmap