I am having a weird issue. I have an ArrayAdapter which I am sorting. This displays properly on my screen, however when I check the actual data source, the contents have not been sorted. How can I ensure that sorting my ListAdapter will also sort my data source?
Collections.sort(quotes, new PercentChangeComparator()); //sort my data source (this isn't necessary)
quotesAdapter.sort(new PercentChangeComparator()); //sort my ListAdapter
Toast toast = Toast.makeText(getApplicationContext(),quotes.get(0).getSymbol(), Toast.LENGTH_SHORT);
toast.show(); //this shows that my data source hasn't been updated, even though my ListAdapter presents my ListView in the correctly sorted order.
For example:
If my data source is [10,9,1,20]
after sorting my ListView will show [1,9,10,20]
but the data source will still be [10,9,1,20]
How can I resolve this?
It's the other way round: sorting your data source will order the ArrayAdapter.
I assume you've done something like this before.
ArrayList<PercentChangeComparator> quotes = getQuotesFromSomewhere();
QuotesAdapter quotesAdapter = new QuotesAdapter(this, R.layout.xxx, quotes);
Then, if you sorted quotes, notifying the adapter that the dataSet has changed should sort the list
Collections.sort(quotes, new PercentChangeComparator());
quotesAdapter.notifyDataSetChanged();
This is working for me, I hope it helps.
One important thing: if you recreate the source array (quotes in this specific example), the Adapter won't read further changes. Thus, if you need to modify the content of your ListView, do:
quotes.clear();
quotes.add(...);
quotes.add(...);
Also, make sure you implemented correctly the Comparator. If you execute this
Collections.sort(quotes, new PercentChangeComparator());
and quotes isn't sorted, then the problem isn't related to the Adapter but to the comparation.
Related
I have a base adapter and 5 array lists which contain different data.
Based on a selection, the contents of the array list are populated on the list view.
At the current moment this functions correctly, however each time, a new selection is made, an entire new adapter is being made and set. Would like to know if there is an alternative way of changing the data displayed without having to set a new adapter.,
I am fully aware of notifyDataSetChanged()
If you are using an ArrayAdapter (just a guess), then there are functions to change the used list (http://developer.android.com/reference/android/widget/ArrayAdapter.html):
clear()
addAll(Collection<t> c)
I am attempting to create a list view which displays different data based on a selection from an a spinner.
My list view is implemented via a base adapter which works fine.
My data is stored in an array list of objects which each contains fields that store individual data and based on the selection from the spinner filters the data stored in the array list and displays it on the list view.
But my question is how can I clear a list view content without clearing its underlying data (stored in the array list) and calling notifyDataSetChanged? I want to be able to preserve the data stored so if the user goes back to a previous selection, the data is still present.
Thank you for your time in reading this.
As your data is stored into an ArrayList, you can simply use:
listView.setAdapter(null);
It will remove the adapter of your ListView, and so, remove all the items displayed.
The data will still be in your ArrayList so if you want to re-display this ListView, you just have to recreate your Adapter and set it again. You can even create a global instance of your BaseAdapter so you only have to create it once.
You could just pass another ArrayList to the adapter with the new data to show inside the ListView.
Or, you could create two ArrayList inside the Adapter, one named original which contains the original elements of the ListView and another currentElements which will be the real ArrayList to show changed as you want. With some custom methods, you can choose to switch between the two versions of the ArrayList.
It's could be the same concept of an Adapter which implement a Filter.
Anyway if i understand your problem, you want to change the content of a ListView based on the selection. Well, in this case i would send another ArrayList to show.
Create a list of adapters, one adapter for each selection. Then simply switch to a different adapter when the selection is changed with listView.setAdapter(adapter)
I am realy new to android, but I am impressed with spinners. I just want to know if it is possible to have a spinner (populated with cities) and when user selects a city, then refresh and show records relevant to that city?
I will handle the json parsing and all, but I want to know if it is possible? or is there a better way for city filtering? any examples would be highly appreciated
Yes it is possible. Once you changed the data of your first spinner, store your second spinner adapter with new data and then call the notifyDataSetChanged() method of your second spinner adapter.
For example I assume you stores the data in list. Before stores the new data clear your previous data using list.clear() and then store the new data. Then call
spinnerAdapter.notifyDataSetChanged();
I hope this will help you.
I have a ListView (actually a ListFragment, but I believe they are functionally essentially the same) that is backed by a SimpleCursorAdapter. When the fragment is loaded, it is passed a long[] of ids that represent the items to be checked by default. So when the data has finished loading from the database, I need to "check" the items in the view that have are contained in the list of ids.
How would I do this? It seems like such a simple task, and I can figure out the logic necessary to perform the actual "checking". But I can't figure out where to place the logic so that it will work properly. I tried putting it in the onViewCreated(View, Bundle) method, but the View passed in is not a ListView and I don't know how to iterate though the views returned by getListView().
Im trying to make an android-app that shows a list of albums and it displays the album-title on each listitem. So far so good.
But I would also need the album-id somehow connected to each listitem to use when I get the next level to list (the album-tracks). How can I in each list item store more values for the items then the displayed text (album-title)?
Right now Im using an ArrayList (to store the album-titles) that is connected to a ArrayAdapter wich use the simple_list_item_1 layout. I get the album-info (title, artist, id) form external xml.
I was thinking on using a multidimensional array but I don't know how to connect it to the ArrayAdapter since it only is expecting an array?
Any suggestions on how I should tackle this?
Instead of using simple list adapter you should use android custom list view.
Check out this link
Hope this will help you.
try Android sample demo project
http://developer.android.com/resources/tutorials/views/hello-gridview.html
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/index.html