First some background info and parameters of what I am looking for.
- The application has several activities that need access to a single list. I am using the Application class in order to maintain this list across all of the activities.
- The list is a list of a custom object.
- There is one activity that has a ListView to show the items in this list.
- There are other activities that need to be able to access this list, but do not show a ListView.
- The list may be sorted or filtered.
- The list can be updated (add, remove, update content) from a background thread at any time and those changes should be reflected immediately when viewing the list.
So far my approach is to have the list managed via methods in the Application, including managing any updates to the list. If the user is currently in the Activity with the ListView in it, it is listening for updates to the list. When it hears an update it copies the list from the Application and sends it to the ListViews custom ArrayAdapter. The ArrayAdapter takes this list copy and replaces the base list, calls the current sort and filter on it and then replaces the list that is actually displayed in the list and calls notifyDataSetChanged(). This is all to avoid any issues the Adapter will have if the list changes from off the UI Thread or without having notifyDataSetChanged() called.
This all works just fine, the problem is that with this approach there are multiple copies of the same list which decreases the usability of this list in other places and creates unneeded memory use. It also can create a lot of copying if a lot of updates come in a certain interval.
So I am looking to see if anyone else has any approaches that would allow the ListView to reference the main list source directly but still avoid issues with updating from off the UI thread?
Thanks
I don’t know that my approach is any better. I create an Adapter, which holds a LinkedList of items. I keep a static reference to that Adapter, and when updates are needed, the other Activities,etc. update the List and call adapter.notifyDataSetChanged()
I think this only uses one copy of the data.