Michael Smith
Michael Smith

Reputation: 29

How to get a variable that is inside onItemSelected method on Android?

I'm trying to get the value of an item that is inside a Spinner, and then I want to send it to a database. However, the code: String variable = spinner_name.getSelectedItem().toString() does not work at all

So it seems the best way is through of the method:

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    String text = parent.getItemAtPosition(position).toString();
    Toast.makeText(parent.getContext(), text, Toast.LENGTH_LONG).show();
}

But how do you get the value of this variable called text, which is inside the above method, to insert into another method?

Upvotes: 0

Views: 101

Answers (1)

Filipe Oliveira
Filipe Oliveira

Reputation: 1013

Create a CustomAdapter for your spinner, pass a callback as parameter when creating the customAdapter. Once you click on a item on spinner, because of callback, you'll have the item on your frag/activity.

From there you can call any method you want.

Upvotes: 1

Related Questions