Create a class to represent a gang:
public class Turma {
private String nome;
private int id;
public Turma(int id, String nome){
this.id = id;
this.nome = nome;
}
public int getId(){
return id;
}
public String getNome(){
return nome;
}
@Override
public String toString()
{
return nome;
}
}
The method that gets the classes from the bank should return a ArrayList<Turma>
:
public ArrayList<Turma> getTurmas(){
ArrayList<Turma> turmas = new ArrayList<Turma>();
Cursor cursor = db.query("turma", null, null, null, null, null, null);
if(cursor != null && cursor.moveToFirst()){
do{
int id = cursor.getInt(0);
String nome = cursor.getString(1);
Turma turma = new Turma(id, nome);
turmas.add(turma);
}while(cursor.moveToNext());
}
return turmas;
}
Where db
is an instance of Sqlitedatabase obtained by the method getReadableDatabase()
of its inherited class of Sqliteopenhelper
Use that Arraylist in the construction of Adapter:
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, turmasArray);
The Adapter will use the method toString()
class Gang to obtain the string that will appear on Spinner.
You can get the selected class on Spinner in this way:
Turma turmaSelecionada = ((Turma)spinner.getSelectedItem());
In this link: https://answall.com/questions/342927/popular-spinner-comdados-do-sqlite
– Claudio Eckert