Android: 1 Adapter to multiple Array Lists - java

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)

Related

Android: How to clear a list view without removing data from the array list?

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)

Multiple lists in one ListView

I have a series of buttons that I would like the user to press in order to show a different list in the list view. Say, button one brings up list one. User adds a few items to list one and then presses button two to open list two; user then adds a few items to that list.
How would I go about having multiple list that share a single list view? Also, how would I have the data of one list be saved so when the user switches lists... they can switch back to the original and have their items still presented in the list?
Thank you!
I guess what you are looking for is ExpandableListView
here is an example too
You could have multiple lists stored in some type of array. A list could be used for each list, and then when you are setting the listview's adapter, pass the current list object. Of course, after each item is added during the adding process(when user is adding items) you would need to add the items to the list, and then reset the listview. And as long as you kept the lists in context the entire time they would not be GarbageCollected. Once the user is done with the lists, you would need to save the lists somehow, unless this is just a temporary context where everytime you revisit the page it shows the default value for the lists.

How to reference a position in a Set?

How can I reference a position within a Set? Like with an array: array[5];
I am developing an android soundboard that has a favorites tab. The favorites tab uses a listview . When a user adds a favorite, the text of the button is added to the set. Now I have setup a contextmenu for the list view to allow the user to delete an item by long pressing, and pushing delete. The context menu passes the position of the list view that triggered the context menu and I am trying to delete that position.
You cannot. Sets have no notion of order, but concrete implementations may.
However, all sets implement toArray(), and
If this set makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order.
What's your underlying data structure?
You can't - a Set does not have indexes for its elements. You have two options, depending on your requirements:
use a SetUniqueList from commons-collections, if you want the properties of Set in a List
use Iterator (and a foreach loop) if you want to iterate the Set

Checking items in a ListView by Default

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().

Sorting my ArrayAdapter doesn't change my data source

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.

Categories

Resources