How to get other values from the selected Spinner item than shown?

Asked

Viewed 42 times

1

Doubt:

How do I find the value of the field handle concerning the nome that was selected in the Spinner ?

Setting:

Table clientes

Campos _id, handle, nome

The ArrayList of Spinner is established as follows:

public ArrayList<String> getClientes(){

        sqLiteDatabase = banco.getReadableDatabase();
        ArrayList<String> clientes = new ArrayList<>();
        Cursor cursor = sqLiteDatabase.rawQuery("SELECT nome FROM clientes ORDER BY nome", null);
        if(cursor != null && cursor.moveToFirst()){
            do{
                clientes.add(cursor.getString(cursor.getColumnIndex("nome")));
            }while(cursor.moveToNext());
        }
        return clientes;
    }

1 answer

1


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 !

  • 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 ?

  • 1

    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);

  • Really, it is more laborious and disorganized to my view ! Thanks ramaral !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.