Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple ListAdapters or a single one, using filtering (Android Performance)

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:

  • remove not matching items from the list and add all matching (but not included) items
  • clear the whole list, add all matching items

Again, to clarify the structure of the App data:

  • Database with raw simple entries (with IDs + title + ...)
  • HashSet with complex entries, created once from DB, readonly + always all entries
  • ArrayList of current entries shown in a listView

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:

  1. Selecting N items (ID only) from DB with "title LIKE"
  2. Iterating a list of 200 items with a "title.contains()" and using only matches
  3. Removing 100 items from an arraylist SHOWN by an list view
  4. Removing 100 items from an arraylist not shown, then connect and show

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

like image 487
Christian Ruppert Avatar asked Oct 10 '22 07:10

Christian Ruppert


1 Answers

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

  • your adapter stores an ArrayList mAllObjects of all entries (for the "all" tab).
  • your adapter stores an ArrayList mRecentObject of recent entries (for the "recent" tab)
  • your adapter stores an ArrayList mMatchObject of matching entries (for the "search" tab)
  • your adapter has two filters
    • the recent filter returns the mRecentObject list (and creates it if it does not exist already)
    • the match filter creates a new mMatchObject list and adds matching elements. There is no optimization to be done here. the delete()method on an ArrayList is O(n).
like image 81
rds Avatar answered Oct 13 '22 12:10

rds