1
How do I add the values in this Spinner ?
I need to create a Spinner
containing 2 "fields" (Cod, option).
Default values will be added, but later the codes cannot be changed, so I need the field cod
when adding other values, I will have no problems.
For that a ArrayList
using a specific class:
public class ArrayPadrao {
private int id;
private int cod;
private String opcao;
public ArrayPadrao(int cod, String opcao) {
this.id = cod;
this.opcao = opcao;
}
public int getCod() {
return cod;
}
public String opcao() {
return opcao;
}
//O que este método retornar é o que Spinner mostrará.
@Override
public String toString() {
return opcao;
}
}
I tried in some ways but without success:
ArrayList<ArrayPadrao> lista = new ArrayList<>();
lista.add(1, "aaa");
I saw some different options (https://stackoverflow.com/questions/13738441/how-to-initialize-a-two-column-arraylist) with List<>
and Map + HashMap
but I would like opinions as to the best way for it.
Code working:
public void spinnerTipo() {
ArrayList<ArrayPadrao> lista = new ArrayList<>();
lista.add(new ArrayPadrao(1, "aaa"));
lista.add(new ArrayPadrao(2, "bbb"));
lista.add(new ArrayPadrao(3, "ccc"));
ArrayList<ArrayPadrao> tipos = lista;
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, tipos);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnTipo.setAdapter(adapter);
}
It’s unclear what you want to ask. What’s wrong with
lista.add(1, "aaa");
?– ramaral
It does not accept. It says that can only 1 argument. I think even having the specific class, he does not accept... I think would have to create 1 obj for each insertion.
– rbz