Listview does not load all items

Asked

Viewed 471 times

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?

  • You say the split with ";" there in the Arraylist values?

  • yes, enter the values that are in Arraylist

  • Updated. Try commenting on the part of Else that avoided the repetition of items, but I touched something else, because the error persists.

1 answer

2


You are passing to the super only two String in objects.get(0). Change to:

public Adaptador(Context context, int textViewResourceId, ArrayList<String[]> objects) {
    super(context, textViewResourceId, objects);
    valores = objects;
}

Note that in the getView is used valores but in the builder is passed only two String. The adapter will use getCount() which has the amount of items in its internal list (as are two String, getCount() = 2) and this will only read two values from the list valores.

Also in class:

public class Adaptador extends ArrayAdapter<String>

Use String[] to the extends:

public class Adaptador extends ArrayAdapter<String[]>

This can avoid future mistakes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.