Spinner to display the selecteditem - android

I'm wondering if there's a setText() method in spinners? What I want to do is display the selected item in a spinner (it's saved via sharedpreferences.) so the next time, the user displays the saved data, it should display in the spinner. How can I do that? I already know how to do it an edittext like this : age.setText(age);
But wondering how to show it in spinners.

There is no such method in Spinner class.
You need to implement a SpinnerAdapter which will be responsible for creating the items of the Spinnerand attach it via the setAdapter() method.
The official Spinners guide may help you: http://developer.android.com/guide/topics/ui/controls/spinner.html

Related

How to maintain the state of a Spinner selection?

I have spinners in my fragment. Selecting each option of my spinners gives me different scores fetching from database. Now, I don't want to fetch the data from database if I select the same option from spinner consecutively. In my current code it is fetching the data on each selection. Is there any way that I can maintain the previous selection of spinners, so that I can give a check to database hit. Thanks.
I tried to solve the above problem and was able to find a good solution. I created a custom spinner taking a reference from this answer https://stackoverflow.com/a/11227847/2734855
The overridden method setSelection(int position) of Spinner class actually gives the event hit back to the OnItemSelectedListener which helps to track the previous spinner selection.

Why can OnItemClickListener not work with a Spinner?

I want to know if there is a specific reason why you cannot use OnItemClickListener with a spinner in Android? Looking through older posts ( setOnItemClickListener Not Works with Spinner and I have an error: setOnItemClickListener cannot be used with a spinner, what is wrong?) it seems as though people don't even bother with OnItemClickListener and rather go straight for OnItemSelectedListener. Why does Eclipse give OnItemClickListener as an option for a spinner if it can never work?
Some context- I'm not really too bothered by which method I use. As soon as the user selects an item from the spinner, I want to make a second spinner visible and populate it from the database based on the option selected in the first spinner. Now when I use OnItemSelectedListener, the second spinner is immediately set to visible. Is there a workaround for this?
Why does Eclipse give OnItemClickListener as an option for a spinner if it can never work?
Because everything extending AdapterView has setOnItemClickListener(). Just because some subclasses ignore it does not mean that the setter magically disappears.
Is there a workaround for this?
There is no workaround, because there is no problem, because it is doing exactly what you say you want to have happen ("I want to make a second spinner visible and populate it from the database based on the option selected in the first spinner"). A Spinner always has a selection if there is data to select from. Hence, your second Spinner should always be visible, since it is "based on the option selected in the first spinner", and there will always be an "option selected in the first spinner".
It sounds like what you really want is to only have the second Spinner visible for specific selections in the first Spinner, where you set up the first Spinner where there is some selection that, in business terms if not technical terms, means "no selection". You are welcome to do this, but then you will have to implement the logic to handle this, only showing the second Spinner when an appropriate value is selected in the first Spinner.

Android spinners populate with data

I have three spinners in my activity. based on the item selected in first spinner the second spinner is populated with a new set of values. till here the program works fine. now i want that based on selection of item on 2nd spinner the third spinner should be populated. but i am not able to achieve this.
As you can do on the first spinner and second spinner. use secondSpinner.setOnItemSelectedListener(new OnItemSelectedListener() and then create and setAdapter to the third spinner.
see this question might help

How to create spinner with heading in it?

I am just thinking to create like below in spinner, but i am not sure how to make it work
Fruits (heading unclickable)
Apple
Mango
Orange
Cars (heading unclickable)
BMW
Lenova
Is that possible in Spinner or if you know any other method to create like this, then it would be great.
I am getting those details from the local database. Heading from one table and records from one table.
I am looking for any example or solution.
Thanks for your help guys.
After long time i found a way to do this. I achieved by combining a Button and Expandable ListView. What i have done is i created a button like spinner. OnClick I launched a popup Activity with Expandable list view in it by calling startActivityForResult() which returns the selected value. Finally i replaced the button text with the selected value.
If you looking for Expandable ListView Tutorial you can click below link.
(http://androidtrainningcenter.blogspot.co.uk/2012/07/android-expandable-listview-simple.html).
Take a look at the Spinner example in the Hello Views tutorial. They accomplish this by using a TextView for the heading, and the spinner for the selectable list of options.

How do you retrieve the array adapter currently set for a spinner? Android OS, Droid

Looking to create a menu toggle that switches between two possible arrays for a spinner. For example, if the spinner is set to show array A, then when I press this menu button, I want the spinner to be set to array B. If I press it again, I want it to be set back to array A.
I can handle the if/then statements and all, but how do I call the spinner's array adapter? I know how to call its value using getSelectedItemPosition(); but is there a similar method for retrieving WHICH array it is currently set to?
I think AdapterView#getAdapter() is inherited by android.widget.Spinner?
What you're seeing is that AdapterView#getAdapter() is an abstract method. You want to be using AbsSpinner#getAdapter() which is where getAdapter() is actually implemented. This will return a SpinnerAdapter.

Categories

Resources