Try to focus on general "dos and donts" regarding lists, e.g. is adding and removing 30 items of a 200 item list better then clearing and repopulating? Or any other tips in this area - I cant really try on my phone, to fast for that :-)
Is there any way to calculate the memory overhead / calculation power of list operations. The background is as following:
I have a list view on a page, the page has e.g. 3 Tabs at the bottom (All, Search, Recent). Now if you click on a tab, the listview should show you the approriate items.
There are two different approaches now, one is:
Use a single ListAdapter, filter the items accordingly
- If you click All, just put all items from the DB into it
- If you click Recent, just put the items which meet the requirements
Use two (three..) ListAdapters, one for each category
- If you click All, setAdapter() of list to the approriate one
- If you click Recent, setAdapter() to appropriate one
We are talking about a list of 200 items, which are complex objects created out of a database. When e.g. searching for an item, you enter part of the title, and the list should only show the appropriate items. The items will not be recreated, I would query for the IDs only, and use the buffered items (see later on datastructure).
What I am also not sure about is "where to filter", I could do it in the database (select from where title LIKE abc) and then EITHER:
Again, to clarify the structure of the App data:
I hope you get my drift, I am trying to get a feel for "expensive" operations. Perhaps, as a last motivation to answer, I will write some cases down, and you can give an opinion about how costly they are:
Thanks for any feedback, or any tips for bad practices. Especially possible event trigger problems by working on visible list elements, instead of doing it first "in the background" and then setting a new ListAdapter
I see you have accepted an answer already, but I think I don't agree, because ArrayList
has to copy all elements, if in the middle is an element added or removed.
I understand you already have a HashSet
with all entries.
In that case, I believe the most efficient adapter is a custom ListAdapter
inspired from ArrayAdapter
ArrayList mAllObjects
of all entries (for the "all" tab).ArrayList mRecentObject
of recent entries (for the "recent" tab)ArrayList mMatchObject
of matching entries (for the "search" tab)mRecentObject
list (and creates it if it does not exist already)mMatchObject
list and adds matching elements. There is no optimization to be done here. the delete()
method on an ArrayList is O(n).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With