ListView does not load all items

0

I'm using ListView in my project, it was doubling the values, until I put the ELSE of that 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)

Following codes.

Calling the adapter in LoadList.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 above code snippet 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 that are shown in the ListView are

Values.get (0) [0] Hamburger 1

values.get (0) [1] R $ 14,90

Values.get (1) [0] Hamburger 2

values.get (1) [1] R $ 16,90

Adapter.java class that fills the 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;
    }
}
    
asked by anonymous 05.02.2017 / 00:13

1 answer

2

You are moving to 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 no getView is used valores but in the constructor only two String is passed. The adapter will use getCount() that has the amount of items in its internal list (as they are two String , getCount() = 2) and will only read two values from the valores list.

Also in class:

public class Adaptador extends ArrayAdapter<String>

Use String[] for extends :

public class Adaptador extends ArrayAdapter<String[]>

This may prevent future errors.

    
05.02.2017 / 02:59