I'm having trouble setting up a custom spinner in my application. Here is an image of how it appears:
Followmycode:
Adapter.java
publicclassProfissionalCategoriaAdapterextendsArrayAdapter<ProfissionalCategoria>{privateContextcontext;privateArrayList<ProfissionalCategoria>lista;publicProfissionalCategoriaAdapter(Contextcontext,ArrayList<ProfissionalCategoria>lista){super(context,0,lista);this.context=context;this.lista=lista;}@OverridepublicViewgetView(intposition,ViewconvertView,ViewGroupparent){finalProfissionalCategoriaitemPosicao=this.lista.get(position);convertView=LayoutInflater.from(this.context).inflate(R.layout.profissional_cat_item_spinner,null);finalViewlayout=convertView;TextViewtextView_categoriaNome=(TextView)convertView.findViewById(R.id.textView_profissionalCatItemSpinner);textView_categoriaNome.setText(itemPosicao.getNome());returnconvertView;}}
Activitywherethespinnershouldbeshown:
this.spinner_profissionalCategoria=(Spinner)myView.findViewById(R.id.spinner_profissionalCategoria);ArrayList<ProfissionalCategoria>lista=newDAOProfissionalCategoria(getActivity()).buscarCategoria(profissionalCategoria);ProfissionalCategoriaAdapterprofissionalCategoriaAdapter=newProfissionalCategoriaAdapter(getActivity(),lista);profissionalCategoriaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);spinner_profissionalCategoria.setAdapter(profissionalCategoriaAdapter);spinner_profissionalCategoria.setPrompt("Escolha");
Professional Class Category
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 information you need is there.
Solution:
I created a method in DAO to fetch the data and return it 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 the 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's attention.