chanding contents of spinners run-time (android) - android

I am trying to make a form with 2 spinners, I want the content of the second spinner to be determined by the value of the first spinner. This would entail a runtime determination of a spinners content whereas I am used to declaring the spinner contents using array adapters.
How do I set the adapter for spinner 2 only after user has chosen value for spinner 1???
Thanks

To resolve this, you can have a data source array:
Initialize with empty, and set this array to adapter data source.
on Selection of first spinner, reset the data source array according to new selection and invoke notifyDataSetChanged() method.

Related

How to set updated value to Multiple spinner using same array?

I am creating spinner dynamically as user enter value in textField.
If user enter three then three spinner get created and all these spinner use same array.
If user select one value of spinner then these value will be deducted from other spinner list
I use onItemStateChangeListener but problem is that when i remove value from array then other spinner has also use same array so changes are reflected in all spinner so i am not geting desire output .
I want the element to display then remove but not show in next spinner list but not getting any solution.
how can i overcome these problem,please help me
You must have to call spinnerAdapter.notifyDataSetChanged(); or spinner.setAdapter(spinnerAdapter); after removing element from array, for next spinners.

How to add data in two spinners dynamically from string.xml in android?

I want to add data in spinner dynamically.
Like I have 3 spinners. Data of first spinner is fixed.
Depending on data selection from spinner 1 the data of second spinner will be fixed. And depending on selected data from spinner 2, the data of 3rd spinner will be fixed.
And I want to get data from string-array. How to add this?

ListView Row Containing Multiple Dynamic Spinners

I have multiple Spinners inside listview control at particular row.
I fill all spinners dynamically from WebService and all spinner values can change on selected item.
Exp. I have 3 spinner of country and State and City then all this 3 spinners fill from WS and while user select country then on countryselecteditem change all data of state and city. And on State selected item city data change...
And also I scroll and go on that row at that time getView() call all 3 selected item and fill all adapters by selected item.
How can I handle 3 spinners inside listview?
Design a ListItem Layout containing 3 spinners of your wish and then set it to ListView Adapter.
Then Take An SubClassArrayAdapter with Generic Type of a POJO Class containing 3 IDs (if required take more) like below and set Adapter to ListView.
class SpinnersStateInListItem
{
private int countrySelectedPosition=0;
private int stateSelectedPosition=0;
private int citySelectedPosition=0;
//here you need to generate respective getters and setter methods.
}
//do the below code to set the SubClassArrayAdapter to listview which you derive from ArrayAdapter class in which you customize your views
SubClassArrayAdapter<SpinnersStateInListItem> = new SubClassArrayAdapter<SpinnersStateInListItem>(context,resourceid,listof SpinnersStateInListItem Generic type);
inside the getView() of your SubClassArrayAdapter Class, create 3 spinner objects and set array adapter to the first Spinner and then set OnItemSelectedListeners to 3 spinners.
Then in the first Spinner OnItemSelected method set the ArrayAdapter to 2nd Spinner which will contain the list of State as per Country selected in the first spinner. Then set array Adapter to 3rd Spinner inside 2nd Spinners OnItemSelected method which contain the list of cities as per country selected and state selected. Here you should filter the data as per selected items. When Items are selected immediately save them to SpinnersStateInListItem respective object so that even if you reload the listview all the items state will remain as it is. for example as below.
OnItemSelected()
{
if(v==firstspinner)
{
listofSpinnersStateinListItem.get(listitemposition).setCountrySelectedPosition(spinnerselectposition);
}
}
in the sameway as above for state and cities too.
Initially you fetch the Data of first country and set it to the First Spinner which simultaneously fetch the first country states and then first country, first state related cities.
Hope this will help you.

Adding Spinner to Listview Header and popuate values dynamically

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!

Android ListView CHOICE_MODE_MULTIPLE, how to set checked index?

I'm using the cool feature of the ListView to show a checkbox next to the item in the ListView.
I bind my list to an array of strings.
The onClick and onSelectedItem listeners get called fine, in this way I know the index of the "string" checked (or unchecked).
I'm storing all the checked strings into preferences (as a comma-concatenated-string), and everytime the activity becomes visible I would like to set the checked items back in the listview.
Is there a way of doing it? or the CHOICE_MODE_MULTIPLE doesn't allow to set the checked items?
note: I'm not using a custom view, since what I want to display is just a string and a checkbox. I've tried setSelection(index) but it should set the onlyone selected (highlighted) row.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,names);
m_playlists_list.setAdapter(adapter);
m_playlists_list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Use the setItemChecked method of ListView
Sets the checked state of the specified position. The result is only valid if the choice mode has been set to CHOICE_MODE_SINGLE or CHOICE_MODE_MULTIPLE.

Categories

Resources