Estoy usando listview para agregar listas en mi aplicación de Android. cuando estoy usando el bucle for en hashmap, muestra la lista pero todos los datos están en la lista final, por ejemplo, el siguiente código funciona bien.
products = new ArrayList<Cash>(products);
HashMap<String,String> temp=new HashMap<String, String>();
list=new ArrayList<HashMap<String,String>>();
temp.put("item1", "id1";
temp.put("item2", "name1");
temp.put("item3", "qty1");
temp.put("item4", "type1");
temp.put("item5", "quantity1");
temp.put("item6", "amt1");
list.add(temp);
temp1.put("item1", "id2";
temp1.put("item2", "name2");
temp1.put("item3", "qty2");
temp1.put("item4", "type2");
temp1.put("item5", "quantity2");
temp1.put("item6", "amt2");
list.add(temp1);
ListViewAdapters adapter1=new ListViewAdapters(getActivity(), list);
listView.setAdapter(adapter1);
Funciona tan bien, pero cuando quiero agregar varios elementos usando for loop. El código se muestra a continuación. Los productos es la lista de matrices que tiene los elementos.
products = new ArrayList<Cash>(products);
HashMap<String,String> temp=new HashMap<String, String>();
list=new ArrayList<HashMap<String,String>>();
for (int i=0; i<products.size();i++){
Log.d("LOG","Product size:"+products.size());
Log.d("LOG","Product name:"+products.get(i).getName());
Log.d("LOG","Product amt:"+products.get(i).getAmount());
temp.put("item1", String.valueOf(i));
temp.put("item2", products.get(i).getName());
temp.put("item3", products.get(i).getQuantity());
temp.put("item4", products.get(i).getType());
temp.put("item5", products.get(i).getQuantity());
temp.put("item6", products.get(i).getAmount());
list.add(i, temp);
//list.
}
ListViewAdapters adapter1=new ListViewAdapters(getActivity(), list);
listView.setAdapter(adapter1);
La salida que quiero
Id1 nombre1 cantidad1 tipo1 cantidad1 amt1
id2 nombre2 cantidad2 tipo2 cantidad2 amt2
Pero obtener salida es
Id2 nombre2 cantidad2 tipo2 cantidad2 amt2
id2 nombre2 cantidad2 tipo2 cantidad2 amt2
2 respuestas
Es porque está actualizando HashMap
en cada iteración, en su lugar, cree una nueva instancia como en
for (int i=0; i<products.size();i++){
Log.d("LOG","Product size:"+products.size());
Log.d("LOG","Product name:"+products.get(i).getName());
Log.d("LOG","Product amt:"+products.get(i).getAmount());
temp = new HashMap<String,String>(); //<-- add this line
temp.put("item1", String.valueOf(i));
temp.put("item2", products.get(i).getName());
temp.put("item3", products.get(i).getQuantity());
temp.put("item4", products.get(i).getType());
temp.put("item5", products.get(i).getQuantity());
temp.put("item6", products.get(i).getAmount());
list.add(i, temp);
//list.
}
Inicialice HashMap
antes de agregar nuevos valores. Verifique a continuación:
temp = new HashMap ();
for (int i=0; i<products.size();i++){
temp = new HashMap<String, String>();
Log.d("LOG","Product size:"+products.size());
Log.d("LOG","Product name:"+products.get(i).getName());
Log.d("LOG","Product amt:"+products.get(i).getAmount());
temp.put("item1", String.valueOf(i));
temp.put("item2", products.get(i).getName());
temp.put("item3", products.get(i).getQuantity());
temp.put("item4", products.get(i).getType());
temp.put("item5", products.get(i).getQuantity());
temp.put("item6", products.get(i).getAmount());
list.add(i, temp);
}
Nuevas preguntas
java
Java es un lenguaje de programación de alto nivel. Utilice esta etiqueta cuando tenga problemas para usar o comprender el idioma en sí. Esta etiqueta rara vez se usa sola y se usa con mayor frecuencia junto con [spring], [spring-boot], [jakarta-ee], [android], [javafx], [hadoop], [gradle] y [maven].