2
I’m having trouble setting up a custom spinner in my application. Follow below picture of how it appears:
Follows my code:
Java adapter.
public class ProfissionalCategoriaAdapter extends ArrayAdapter<ProfissionalCategoria> {
private Context context;
private ArrayList<ProfissionalCategoria> lista;
public ProfissionalCategoriaAdapter(Context context, ArrayList<ProfissionalCategoria> lista) {
super(context, 0, lista);
this.context = context;
this.lista = lista;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
final ProfissionalCategoria itemPosicao = this.lista.get(position);
convertView = LayoutInflater.from(this.context).inflate(R.layout.profissional_cat_item_spinner,null);
final View layout = convertView;
TextView textView_categoriaNome = (TextView) convertView.findViewById(R.id.textView_profissionalCatItemSpinner);
textView_categoriaNome.setText(itemPosicao.getNome());
return convertView;
}
}
Activity where the spinner should be shown:
this.spinner_profissionalCategoria = (Spinner) myView.findViewById(R.id.spinner_profissionalCategoria);
ArrayList<ProfissionalCategoria> lista = new DAOProfissionalCategoria(getActivity()).buscarCategoria(profissionalCategoria);
ProfissionalCategoriaAdapter profissionalCategoriaAdapter = new ProfissionalCategoriaAdapter(getActivity(),lista);
profissionalCategoriaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_profissionalCategoria.setAdapter(profissionalCategoriaAdapter);
spinner_profissionalCategoria.setPrompt("Escolha");
Classe Professionalcategoria
public class ProfissionalCategoria {
private int autoId;
private String nome;
public int getAutoId() {
return autoId;
}
public void setAutoId(int autoId) {
this.autoId = autoId;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
}
I believe the necessary information is there.
Solution:
I created a method in DAO to fetch the data and return with a different type, see:
Previous method:
public ArrayList<ProfissionalCategoria> buscarCategoria(ProfissionalCategoria profissionalCategoria) {
ArrayList<ProfissionalCategoria> lista = new ArrayList<>();
String[] colunas = new String[]{"autoid", "nome"};
Cursor cursor = this.db.query("ProfissionalCategoria",colunas,null,null,null,null,"nome");
if(cursor.moveToFirst()){
do {
profissionalCategoria = new ProfissionalCategoria();
profissionalCategoria.setAutoId(cursor.getInt(0));
profissionalCategoria.setNome(cursor.getString(1));
lista.add(profissionalCategoria);
} while (cursor.moveToNext());
cursor.close();
}
db.close();
return lista;
}
New method:
public List<String> buscarCatSpinner(){
List<String> list = new ArrayList<String>();
String[] colunas = new String[]{"autoid", "nome"};
Cursor cursor = this.db.query("ProfissionalCategoria",colunas,null,null,null,null,"nome");
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
list.add(cursor.getString(1));//adding 2nd column data
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning lables
return list;
}
And in Activity where it shows the spinner:
DAOProfissionalCategoria db = new DAOProfissionalCategoria(getActivity());
List<String> labels = db.buscarCatSpinner();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, labels);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_profissionalCategoria.setAdapter(dataAdapter);
Thanks for the help everyone attention.
In fact it seems to be showing the object name, not the property value returned by itemPosicao.getName(). Try to put a Breakpoint and debug there the Getview function being loaded in itemPosition.
– Celso Marigo Jr
Hello Celso, then at breakpoint it shows the following: List: size=4, position=0 .
– Davi Queiroz
David, if you have found a solution other than the answers, you should publish it as an answer rather than adding it to the question, so your solution will be more evident.
– user28595