The information you can get from the selected Spinner item is only the one stored in the Arraylist that is used by the Adapter.
As far as I can tell, Arraylist only guards the column name.
Define a class that represents a client:
public class Cliente{
private int id;
private int handle;
private String nome;
public Cliente(int id, int handle, String nome){
this.id = id;
this.handle = handle;
this.nome = nome;
}
public int getId(){
return id;
}
public int getHandle(){
return handle;
}
public String nome(){
return nome;
}
//O que este método retornar é o que Spinner mostrará.
@Override
public String toString()
{
return nome;
}
}
This class must be the type used by Arraylist to be used by Adapter.
Change the method getClientes()
to return ArrayList<Cliente>
:
public ArrayList<Cliente> getClientes(){
sqLiteDatabase = banco.getReadableDatabase();
ArrayList<Cliente> clientes = new ArrayList<>();
Cursor cursor = sqLiteDatabase.rawQuery("SELECT * FROM clientes ORDER BY nome", null);
if(cursor != null && cursor.moveToFirst()){
do{
int id = cursor.getInt(cursor.getColumnIndex("_id")));
int handle = cursor.getInt(cursor.getColumnIndex("handle")));
String nome = cursor.getString(cursor.getColumnIndex("nome")));
Cliente cliente = new Cliente(id, handle, nome);
clientes.add(cliente);
}while(cursor.moveToNext());
}
return clientes;
}
Get the selected Customer with:
Cliente cliente = ((cliente)spinner.getSelectedItem());
and Handle with:
int handle = cliente.getHandle();
Perfect. In addition to working out, it helped me understand other details ! Thank you very much !
– rbz
ramaral, just a doubt... there will always have to be created a class for Arraylist, or there is an alternative form ? The
ArrayList<?>
could work ?– rbz
ArrayList<?>
declares an Arraylist that can contain any type(class) of object. However, this type must exist, it must have been defined. In addition it will be necessary to do the cast in order to be able to access its members:ArrayList<?> list = new ArrayList();
list.add(new Tipo1());
list.add(new Tipo2());
Tipo1 tipo1 = (Tipo1)list.get(0);
– ramaral
Really, it is more laborious and disorganized to my view ! Thanks ramaral !
– rbz