0

Im having some trouble with my ArrayList, im working on an app for my restaurant, i got the AutoCompleteTextView with my dishes, and when i select one of them it puts into a list and shows with an Adapter, the problem shows when i try to put another dish to my list of order, im from Guatemala so my examples will be in Spanish, by example im adding a dish called "Hamburguesa de pollo" and it adds correctly to te ArrayList, but when i add a Dish called "Ravioles" it removes the "Hamburguesa de pollo" and shows me two items that says "Ravioles" like this.

Problem And if i add more items the las times erase the old ones and show me the same over the entire list.

This is my declaration above the onCreate method

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

This is the listener of the AutoCompleteTexView

textViewPlatos.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                textPlatos=textViewPlatos.getText().toString();
                getJSON3();

                textViewPlatos.setText("");

            }
        });

This works whit an AsyncTask and goes to this method, that does the query to get de data, and then add the item to the list, and shows to te adapter.

private void mostrarOrden3(){
        JSONObject jsonObject = null;

        try {
            jsonObject = new JSONObject(JSON_STRING);
            JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);



                JSONObject jo = result.getJSONObject(result.length()-1);
                String id = jo.getString(Config.TAG_ID_PLATOS);
                String nombre = jo.getString(Config.TAG_NOMBRE_PLATOS);
                String precio = jo.getString(Config.TAG_PRECIO_PLATOS);
                String descripcion = jo.getString(Config.TAG_DESCRIPCION_PLATOS);
                String nombrecategoria = jo.getString(Config.TAG_NOMBRECATEGORIA_PLATOS);



                comandas.put(Config.TAG_ID_PLATOS,id);
                comandas.put(Config.TAG_NOMBRE_PLATOS,nombre);
                comandas.put(Config.TAG_PRECIO_PLATOS,precio);
                comandas.put(Config.TAG_DESCRIPCION_PLATOS,descripcion);
                comandas.put(Config.TAG_NOMBRECATEGORIA_PLATOS,nombrecategoria);



                list.add(comandas);

          //  }


        } catch (JSONException e) {
            e.printStackTrace();
        }

        ListAdapter adapter = new SimpleAdapter(

                NuevaOrdenActivity.this, list, R.layout.list_item_platos,
                new String[]{Config.TAG_NOMBRE_PLATOS},
                new int[]{ R.id.name});

        listView.setAdapter(adapter);


    }

I dont really know when the code erase the names and replaces all of them with the new item. I made a Debug and the items in list is really overwrited, over and over again, to the end it has a list of the same last items, sorry for my english, and thanks for helping me.

Vishal Yadav
  • 3,642
  • 3
  • 25
  • 42
  • Each time you add to the list, you need to create a new `HashMap` instance - e.g., `comandas = new HashMap<>();` - before you `put()` the new values. Otherwise, you're just adding the same `HashMap` to the list over and over again, and changing its values to the newest ones each time. – Mike M. Oct 27 '17 at 05:08
  • @MikeM. Thank you it resolve te problem, and thanks for the explanation. I dont really know what i have to do when my question has marked as duplicate, i should delete it? – Tomás Méndez Oct 30 '17 at 17:50
  • No, you don't have to delete it. We just mark duplicates for questions that have been answered previously in one form or another. If someone in the future searches for a similar issue, and they happen to use keywords that bring up your question first, they'll still get pointed to the right solution, since it's linked in the banner at the top now. Glad you got it working. Cheers! – Mike M. Oct 30 '17 at 17:57

0 Answers0