Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android listing design problem with cursors

I have a following situation in my android app.

  1. I have an app that fetches messages from inbox, sent items and drafts based on search keywords. I use to accomplish this by fetching cursors for each manually based on selection by user and then populating them in a custom data holder object. Filter those results based on given keywords and then manually render view with respective data.

Someone suggested that I should use a custom Cursor adapter to bind view and my cursor data. So I tried doing that. Now what I am doing is this:

  1. Fetch individual cursors for inbox, sent items and drafts. Merge them into one using Merge cursor and then pass that back to my CursorAdapter implmentation.
  2. Now where or how do I filter my cursor data based on keywords; because now binding will ensure that they are directly rendered to view on list. Also, some post fetching operation like fetching sender's contact pic and all will be something that I do not want to move to adapter. If I do all this processing in adapter; it'll be heavy and ugly.

How could I have designed it better such that it performs and the responsibilities are shared and distributed.

Any ideas will be helpful.

like image 818
Priyank Avatar asked Feb 25 '26 08:02

Priyank


1 Answers

Using cursors and adapters does not work out well for the most part. Our experience as led down a different technique.

Your best bet is to "pump" the cursors in an AsyncTask into an ArrayList<data-holding object>, then you can do processing and then sort that list as necessary with Collections.sort() and the Comparator of your own construction, and then use ArrayAdapter to present the resulting list. This releases cursors ASAP and stays off the UI thread while doing it and you can sort however you feel like.

Remember always process in the background and avoid ANR!

We use this in all of our apps (14 on The Market) and it works like Butter.

like image 190
escape-llc Avatar answered Feb 28 '26 10:02

escape-llc