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!
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 a GridView of ImageButtons where the user can select an element to add to the ListView which is the following View. The user will need to make multiple selections from the GridView. This means they will have to navigate back and forth between the two Views, adding their selections to the ListView. I need to know how to re-inflate the ListView with the elements that have already been chosen along with the new choice. Basically, I am struggling with how to preserve the list contents and then inflate the contents when another selection is made. I have been trying to use an ArrayAdapter, but I have been unsuccessful.
It's common to overlook the need to use notifyDataSetChanged() when getting to grips with ListView. Below is a basic rundown of how to populate and refresh a ListView.
Create an ArrayList for your list of elements, a ListView to display them in, and an ArrayAdapter to connect them:
private ArrayList<String> mMyElements;
private ListView mMyListView;
private ArrayAdapter<String> mMyArrayAdapter;
Setup your ListView:
mMyListView = (ListView)findViewById(R.id.myListView);
mMyListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); // There are other ChoiceModes available,
// but I'm guessing this is the most likely one you want for your situation.
Setup your ArrayAddapter and assign it to the ListView:
mMyArrayAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_activated_1, mMyElements);
mMyListView.setAdapter(mMyArrayAdapter);
Now you can change what is displayed in the ListView, by changing what is contained in your ArrayList. Use notifyDataSetChanged() to signal to the ArrayAdapter that it needs to update the display of the ListView:
...
// Code which changes the elements contained in the ArrayList
// For example..
myElements.add(x);
myElements.remove(y);
...
// Notify the ArrayAdapter that it's ArrayList has changed.
mMyArrayAdapter.notifyDataSetChanged(); // This line is vital to get the altered ArrayList to display.
mMyListView.clearChoices(); // You may want to clear any old selections from the ListView when you refresh the display.
<Additional>
If by "re-inflate" you mean that your GridView and ListView are in different Activites or Fragments, all you need to do is maintain the ArrayList myElements when navigating between them. You can pass the ArrayList between them in an intent.
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.
my apllication has a listview that contains:
1.Title
2.Image
3.Rating
4.Price
Now i want to implement sorting in my listview ie through a dropdown box or spinner so that i can sort the items alphabetically,rating etc
Please provide me with some code.....
You will have to check for the options selected into the spinner and based on that,you will have to send your sorted data to the listview(if you are using CursorAdapter,then everytime cursor should be fetch in sorting order you want your items to be in) for each of your option.
reloading the listview is the only solution to your problem,AFAIK.
Here is my scenario: I've got a database, where 2 columns are interesting for me. I now created an autocompletetextview where the adapter gehts via SQL the entries of one column.
So far... so good, but what i actually want is, that if the user types sth. into the autocompletefield i notice this and fetch the text. Via SQL I now want to get everey Entry of column 2 where the value of column 1 is contained in the entered text. And this List should be displayed in the adapter. In other words I filter the data by myself via sql but i want them to be shown in the adapter.
The problem is, that the adapter only shows the entries which start with the text in the autocompletefield but I want to show the adapter anytime, as already saied: I filter the data by myself I just want the adapter to display them.
Any ideo how to solve this? Is the AutocompleteTextView the right item to use?
I also thought about using a simple Spinner... but when using a spinner there are 2 main problems. first I would have to make him editable. and second I would need a dropdown instead of the "popup". ...
Another idea of mine to solve this issue is to write an own filter for the autocomplete field... but i've never down this so far and I also couldn't found any examples in the internet to do so.
thanks for your help
I changed the ArrayAdapter method of spinner and i got my custom Spinner.
just change the parameter of this method like
Spinner spinner = (Spinner) findViewById(R.id.Spinner01);
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(This,R.array.statename,R.layout.mylayout);
spinner.setAdapter(adp);
In Which the mylayout file contains
<EditText
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/spinnerTarget"
android:textColor="#FF8B1500"
android:gravity="center"
>
You can use autocompleteTextView and set its adapter to your values from SQLite or from String Array as shown below:
String values[]= {"Orange","Apple","Pineapple"};
AutoCompleteTextView text=(AutoCompleteTextView)findViewById(R.id.a1);
text.setAdapter(new ArrayAdapter<String>
(this,android.R.layout.simple_dropdown_item_1line,values));
for more information on it go to developers site