0

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

4
  • Why adapter initialization in for loop? Commented Jan 6, 2018 at 12:46
  • What is the value of FIRST_COLUMN, SECOND_COLUMN..? Commented Jan 6, 2018 at 12:56
  • i have edited my question. FIRST_COLUMN for productname (pen,book,pencil) , SECOND_COLUMN for quantity (1,1,1), THIRD_COLUMN for price (27.00,15.00,12.00).but those values twise in my list.see logcat Commented Jan 6, 2018 at 13:01
  • 1
    are you sure you need a list of HashMaps? HashMap is a very expensive memory object Commented Jan 6, 2018 at 13:03

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.