ListView Row Containing Multiple Dynamic Spinners - android

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.

Related

How to reload Gridview's Items after changing selected item in Spinner

In my application, I have a Spinner which includes list of categories and GridView to show list of
Images according to selected category.
My question is: "How to reload images on GridView after changing category in Spinner?"
From the code that gets the selected category from the Spinner, get your GridView's adapter and then call clear() on it and then addAll(T... items) with the new category items you want to show.
Keep in mind that this assumes you've created the adapter by passing it a mutable List.
If you created the adapter with an immutable List or T[], then you can create a new adapter and set it on the GridView instead.

Add Items dynamically in ListView without affecting other already added items in Android

How to add Items dynamically in ListView such a way that it does not affect already added Items.
I am using custom ListView which contains TextView, Spinner and EditText. I want that if user has entered some text in EditText and then if he/she adds another items then already added/changed values does not affect anyway. After that, I also want values of all 3 controls on ButtonClick
Please help me for this.
Create adding method whitch will loop your listview adapter's items for the same and if contains this value then call return in add method or some other action, if not contains then add new item to your adapter and call for it notifyDataSetChanged() to update ListView in yuor Activity or Fragment.
void addMethodExample(String someValue) {
for (String item : arrayInYourAdapter)
if (item.equals(someValue)
return;
arrayInYourAdapter.add(someValue);
arrayInYourAdapter.notifyDataSetChanged();
}
It's example of adding String value to the ListView. By this logic write adding method of item in your adapter's items array.

chanding contents of spinners run-time (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.

Android:How to retain change in color of ListView row?

In my one of the ListView activity i am changing color of 1 or more row on click, but i want to retain this change in color of row, when i come back to this activity again. so how to save and retrieve change in color of row in ListActivity
If you want to have your color changed of the clicked rows than, you have to somehow save the state for the rows(if it is clicked or not) as #mak_just4anything suggested to you the best suitable datatype for you is array list of booleans. This list you can save it in sqlite database of maybe use a key value pairs of preferences. But it is very important to note that you need to notify you adapter for the data changed. After the adapter is refreshed ( I guess you will do this in onCreate or in onResume callback) then you will see the listview with right coloring
Take an array of booleans, and make the indexes of items and boolean same, it means you will have boolean array corresponding to the array of items, when ever you click on item change the boolean value to true so it will be saved in that index and whenever you come back you will find the same array with colored items corresponding to the boolean array....!

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