My spinner control is displaying fine no problem. When someone is in create mode they are able to selected a value which I am able to record and store in my database.
But I have a problem when someone is in edit mode and I want to display the list but have one of the items (say the third) selected. I can't see how to do that. All the tutorials talk about how to populate the spinner but not how to preset one as being selected.
this.ddlCategory = (Spinner)this.findViewById(R.id.add_edit_place_ddlCategory);
ArrayAdapter adpt = new ArrayAdapter(this, android.R.layout.simple_spinner_item, categories);
adpt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
this.ddlCategory.setAdapter(adpt);
Please help
Cheryl
You can use one of the setSelection methods to programatically select a value.
Related
I am using a Spinner and ArrayAdapter to populate items in Spinner. I want to make the spinner multi-selectable i.e. user can select multiple items and should also be able to deselect to none. My code is as below,
spnAdapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_multichoice,items);
spnAdapter.setDropDownViewResource(android.R.layout.simple_list_item_multiple_choice);
spnProducts.setAdapter(spnAdapter);
Here the items is an array list.
This code gives me a spinner with check boxes but it does not allow multiple selection. What should I do to make it multiple selection?
Thanks in advance.
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
I just wanna ask how can I create a multi selection spinner in android and the data that will be populated to is from the database. I saw this example here in SO Android Spinner with multiple choice and tried to use it but I don't know what to do next. This is my spinner and adapter and I want to make it in multiselect spinner. Help me please...
spn_CustomerSegment = (Spinner)findViewById(R.id.spn_CustomerSegment);
List<String> ConsumerSeg = databaseHandler.setItemOnConsumerSeg();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(IreportMain.this, android.R.layout.simple_spinner_item, ConsumerSeg);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spn_CustomerSegment.setAdapter(adapter);
I know its not direct answer but you can try using a acrollView (vertical or horizontal) and populate it with checkboxes.
it can give you a nice effect end no extra coding is needed
I have a question about adding spinner to listview header:
How should I get a spinner to showup in the listview header.
How to dynamically populate values in this spinner based on values in the listview.
Description:
I have a listview which populates values from an XML file (placed on the net). This list can be huge based on user selection on previous screen(s) and/ or data in XML file.
I would like to add a spinner at the top of the listview such that user will have an option to perform filter action/ sort action. How should this be achieved?
For example:
If my app shows a list of books available in the library. The spinner should help user to:
1. Sort alphabetically based on book title
2. Sort alphabetically based on author name
3. Filter out only books related to 'War'
4. Filter out only books related to 'History'
5. Filter out only books related to 'Science'
When I am populating the listview, the XML provides details such as 'Type of Book', 'Author', 'Title', etc.
Question is, how do I place a spinner at the top & populate values like, the types (War, History, Crime, etc.) to the Spinner based on the list of books in the listview.
I have tried to be as descriptive as possible. If any further details required, please let me know.
Any help will be highly appreciated.
Thanks a Bunch!
For putting the spinner above the ListView, check this method out: http://developer.android.com/reference/android/widget/ListView.html#addHeaderView%28android.view.View%29
Or you could put a custom title-layout thing about the ListView in the parent layout for the Activity.
More ListView help: http://www.vogella.de/articles/AndroidListView/article.html#listview_overview
As far as populating the spinner, you can have the spinner set to whichever values you want. In the example online( http://developer.android.com/resources/tutorials/views/hello-spinner.html ):
String[] myValues = // Put code here for the values and size of array you want.
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, YOUR_STRING_ARRAY_VARIABLE_HERE, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
It is your job as a programmer to figure out how to take which item is selected in the spinner and make changes to the listView. To update values in a listview check this out: update listview dynamically with adapter
One solution and how I would do it is make a custom Java class for an Entry so that you can take in the XML file and give each entry instance variables or characteristics to sort them by. Then using this data model, make a class EntryList or EntryArray that can handle the sorting of your Entry data objects. Then have a toStringArray() method that will return a string array. In this way you hold on to the data, and allow all of the sorting you need. Each time the spinner is clicked, the corresponding method in EntryList or EntryArray is called, a new String array is put in the adapter, and the user sees the new sorted list.
This was probably a lot to digest, so read up on all of the links, and see if you can implement it. Cheers!
I have an android spinner that's populated by a list of strings using an ArrayAdapter and it operates fine, however because of the way the spinner is displayed I'm running into a display height problem with the list items.
At first glance, it would seem that the ArrayAdapter can use a single layout for displaying options which leads to the problem I'm having. When displaying the current item in the spinner (when the user is not selecting a new item from the list) the spinner pads the text so that the spinner is a reasonable size for clicking on. However, when the user taps on it and brings up the list to select a new item, the list items presented are way too small height-wise. If I use an item layout that presents the list items at a reasonable height, then the spinner itself becomes exorbitantly huge due to its own padding of the list item.
Any ideas on how I can manage the height of these two item display modes so that effectively they display with the same height value instead of the spinner height being larger than the list item display height?
I've run into this issue myself a while ago, and it turned out that I need to use different layouts for dropdown and display
I have this code:
adapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, cGroups,
new String[] {
"name", "_id"
}, new int[] {
android.R.id.text1
});
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Yes, the above answer is correct.
It took me forever to find this, because it's wrong in the sdk samples for 2.2 Android. And I had a hard time accepting that.
Here's a snippet from
samples/android-12/Spinner/src/com/android/example/spinner/SpinnerActivity.java:
this.mAdapter = ArrayAdapter.createFromResource(this, R.array.Planets,
android.R.layout.simple_spinner_dropdown_item);
while it should have android.R.layout.simple_spinner_item there instead and simple_spinner_dropdown_item should only be used for the dropdown items. Otherwise the spinner arrow get streched and it draws dropdown selection circle to the display, too.