K3NN3TH
K3NN3TH

Reputation: 1496

Android - How do I get the value out of the selected spinner object?

Im creating a spinner like so

List<Spinnerobject> list = verificationdata.getAreaList();
ArrayAdapter<Spinnerobject> adapter = new ArrayAdapter<Spinnerobject>(mactivity, android.R.layout.simple_dropdown_item_1line, list);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
msparea.setAdapter(adapter);

My SpinnerObject looks like this:

public class Spinnerobject {
    private  int id;
    private String value;

    public Spinnerobject ( int id , String value ) {
        this.setId(id);
        this.setValue(value);
    }

    private void setId(int id){
        this.id = id;
    }

    public int getId() {
        return this.id;
    }

    private void setValue(String value){
        this.value = value;
    }
    private String getValue () {
        return this.value;
    }

    @Override
    public String toString () {
        return getValue();
    }
}

It holds the id and the value from my database. I am able to reference the ID of the selected item in the spinner object by using this (int) msparea.getSelectedItemId(); but I need to get the value of the selected item in the spinner object. How do I access the Spinnerobject selected value?

Upvotes: 0

Views: 901

Answers (1)

Alexis C.
Alexis C.

Reputation: 93842

How do I access the Spinnerobject selected value?

Just call getSelectedItem and cast the result to Spinnerobject since you filled it with an adapter of Spinnerobject.

Spinnerobject selected = (Spinnerobject) msparea.getSelectedItem();

Upvotes: 2

Related Questions