Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top