0
I’m using Listview in my project, it was duplicating the values, until I put the ELSE of this condition:
if(convertView == null)...
else...
After this it stopped duplicating, but also stopped showing all 12 items, now showing only the first 2. Can anyone identify the problem?
Additional information: - Each item in the list contains 2 information, stored in Arraylist (Product Name and Price)
Follow codes.
Call from the Adapter in Carregarlista.java:
ArrayList<String[]> valores = new ArrayList<String[]>();
...
//apenas para entendimento, código está ok
String tmp = nome+";"+valor;
String[] val = tmp.split(";");
valores.add(val); //valores.get(i) = Array de String [0]nome / [1]valor
Log.e("tamanho valores", String.valueOf(valores.size())); //imprime 12 (correto)
adapter = new Adaptador(CarregarLista.this, R.layout.item_lista, valores);
lista = (ListView) findViewById(R.id.listaProd);
lista.setAdapter(adapter);
...
VARIABLES: Arraylist values ends the code snippet above with the values:
ArrayList valores(12) {
(0): Array[0] Hamburguer 1 Array[1]R$ 14,90
(1): Array[0] Hamburguer 2 Array[1]R$ 16,90
(2): Array[0] Hamburguer 3 Array[1]R$ 14,90
(3): Array[0] Hamburguer 4 Array[1]R$ 16,90
(4): Array[0] Hamburguer 5 Array[1]R$ 14,90
(5): Array[0] Hamburguer 6 Array[1]R$ 16,90
(6): Array[0] Hamburguer 7 Array[1]R$ 14,90
(7): Array[0] Hamburguer 8 Array[1]R$ 16,90
(8): Array[0] Hamburguer 9 Array[1]R$ 14,90
(9): Array[0] Hamburguer 10 Array[1]R$ 16,90
(10): Array[0] Hamburguer 11 Array[1]R$ 14,90
(11): Array[0] Hamburguer 12 Array[1]R$ 16,90
}
The only ones shown in Listview are
get(0)[0] Hamburger 1
get(0)[1] R$ 14,90
get(1)[0] Hamburger 2
get(1)[1] R$ 16,90
Class Adapter.java that fills Listview:
public class Adaptador extends ArrayAdapter<String> {
private ArrayList<String[]> valores;
public Adaptador(Context context, int textViewResourceId, ArrayList<String[]> objects) {
super(context, textViewResourceId, objects.get(0));
valores = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View linha;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
linha = inflater.inflate(R.layout.item_lista_hamburguer, parent, false);
}else {
linha = convertView;
Log.e("debug", "Essa linha aparece 2x no Logcat");
}
Log.e("debug", "Essa linha aparece 4x no Logcat");
TextView valor = (TextView) linha.findViewById(R.id.item_valor);
valor.setText(valores.get(position)[1]);
TextView nome = (TextView) linha.findViewById(R.id.item_nome);
nome.setText(valores.get(position)[0]);
return linha;
}
}
Therefore you are separating the items by a semicolon. It has how you insert also the items here so that the staff can try to reproduce your error?
– viana
You say the split with ";" there in the Arraylist values?
– Jhonatan Pereira
yes, enter the values that are in Arraylist
– viana
Updated. Try commenting on the part of Else that avoided the repetition of items, but I touched something else, because the error persists.
– Jhonatan Pereira