detno29
detno29

Reputation: 2231

How to add an option to the top of an existing spinner in Android

At the moment, I have a category Spinner. When user click to the spinner, a drop down list with different categories such as food, services, education, etc. will be displayed.

I get the categories from the database via a cursor and I am able to display what I get from the database to the spinner.

My question is: How can I add one more options to the spinner, after binding all categories to it? What I want to do is to add "All" option to the top of the drop down list when user clicks to the spinner.

final DBAdapter mDB = new DBAdapter(getActivity());
            mDB.openDB();

            List<String> categoryFilter = mDB.getItemByFilteredCategory();
            ArrayAdapter<String> filterAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, categoryFilter);
            spnFilter.setAdapter(filterAdapter);

Thanks in advance.

Upvotes: 2

Views: 903

Answers (2)

Bram
Bram

Reputation: 4613

Could the insert method of the arrayadapter be of any help?

ArrayAdapter.html#insert(T, int)

filterAdapter.insert("First String", 0); //adds myObject to the first position.

Upvotes: 3

Stephen
Stephen

Reputation: 10079

To add a value to the spinner use setAdapter method and ArrayAdapter Class.

Eg: ArrayAdapter.add(value);

Add all the values in string.

For Eg: Refer these android docs.

<resources>     
<string-array name="planets_array">     
<item>Mercury</item>     
<item>Venus</item></string-array>
</resources>

If you add values in String,Then set the name in a order which you want in top.

Upvotes: 0

Related Questions