Skip to content

An Analysis between BaseAdapters and CursorAdapters

January 13, 2010

Hey everyone,

So now that we’ve gone pretty in depth in our previous examples/tutorials as to what CursorAdapters are, how to implement them, how to create a Custom CursorAdapter, and finally how to create a Custom BaseAdapter, the question remains:

“When should I use which?”

And so I thought I would remark a little on the runtime performance of both. In my experience, using the Custom CursorAdapter is SIGNIFICANTLY faster – although this is largely dependent on what you are trying to accomplish.

If you are trying to create a pretty simple list whose contents do not come from a database of any kind (I would believe that this is rare… but I’m sure it’s still possible) then your best option would definitely be to create some kind of Object[] or ArrayList and to send that into your BaseAdapter for some quick processing and view creation.

However, if you are trying to create a list whose contents come from a database, then it’s much faster to use a Custom CursorAdapter. Why? The answer is easy – the SimpleCursorAdapter is designed and optimized to take in a Cursor and quickly run through the rows of the table it is pointing at. Just consider how you would even implement a BaseAdapter – you would first have to query your database and then iterate through the returned cursor for the desired contents, storing them in some kind of list, and THEN sending that list into your BaseAdapter which would then iterate through your list AGAIN. So yes, when you put it this way, it’s quite obvious that for anything involving a cursor it’s easiest to just throw that cursor into a Custom CursorAdapter for quick and optimized creation of your list.

When put this way, why would anyone ever want to implement a BaseAdapter while querying with a cursor…

Well the answer is simple – sometimes new developers first hear about the BaseAdapter and build a little niche around these kinds of adapters and are too afraid to spend the time picking up on how to create the SimpleCursorAdapter. This is exactly the same situation I was in a few months back and I was simply trying to recreate a person’s contact list using a BaseAdapter when I was constantly met with ANR (i.e. timeout) responses!

And so I hope these posts shed some of the fear / mystery surrounding these Adapters, and allow you to create beautiful custom lists at incredible speeds!

For more on Cursor Adapters, please see http://thinkandroid.wordpress.com/category/android-tutorials/cursoradapter-tutorials/

Happy coding.

- jwei

PS: If people know about even better ways to create BaseAdapters and or SimpleCursorAdapters then I’m all ears! I know the Android SDK optimizes the BaseAdapter by using a ViewHolder, but other than that I’m unaware of any better usages of the BaseAdapter.

About these ads
10 Comments leave one →
  1. Mike permalink
    February 3, 2010 12:46 am

    Thank you, I was not aware of the cursor adapters. This is clearly the right tool for the job when it comes to database bound lists. No need to load all the records and create all those uncessary objects, when you got a multi-directional cursor. This way is more inline with the concept of only loading the records that are visible. I was troubled by the fact that the BaseAdapter could only partially implement this concept.

    • July 25, 2011 2:11 am

      Thankyou for your really good set of posts.

      Now that I am convinced to use a Custom Cursor Adaptor, is the SimpleCursorAdapter limited to only ListViews? Or can I use it with Gridview?

      If so, how would I name the gridview (as I believe the SimpleCursorAdapter looks for ListView with id of ‘list’)?

  2. Kevin permalink
    April 11, 2012 9:53 am

    Can you allow items in a cursor adapter to be reordered and deleted? If so how? I have a drag and drop list view with a delete button on each item when in edit mode. How would I do this with a cursor adapter?

    • May 8, 2012 12:58 pm

      Hey Kevin,

      Yes this is possible – you can simply do the delete in whatever database / content provider the cursor is derived from and then call the notifyDataChange() method.

      - jwei

  3. Harith permalink
    April 14, 2012 2:00 am

    Hi guys, I’m new to Android and have a simple question. Currently I’m working on a products application that allows users to add, remove and browse variety of products. The application uses a predefined XML-based layout file as a template that fills the entire screen to show the current product details. When the user adds a new product, I want to re-use the same layout file but populate it with the new product’s information. Also, I want to keep the previously added products (e.g. an ArrayList) and the user can browse this list by sliding horizontally (left-to-right or right-to-left). Now, what is the best thing to use to represent each product (View, sub-view, …etc.), and how to reuse the same XML-based layout file to display different products’ details. Please excuse my English and thank you in advance for the help

  4. Kou permalink
    May 23, 2012 3:18 am

    You can optimize the custom CursorAdapter by creating in newView() a ViewHolder object and pass to the view’s tag, and by accessing that ViewHolder object in bindView() through getTag().

  5. July 3, 2012 1:34 am

    I agree, that using SimpleCursorAdapter instead of BaseAdapter is much cleaner for data extracting via Cursor. But your thesis, that the only way to use BaseAdapter is to provide a collection of POJO objects into it, looks like arguable point. The alternative way is to share Cursor between custom BaseAdapter and Activity. If a Cursor’s method moveToPosition has a constant complexity then using BaseAdapter instead of SimpleCursorAdapter will not have perfomance penalty. Any comments about this sample: http://androidsamples.blogspot.com/2009/06/how-to-display-thumbnails-of-images.html

  6. Jtnks permalink
    February 12, 2013 9:24 am

    Thank you very much, i was looking for it since some time..

Trackbacks

  1. Can these listviews work? with database content - Android Forums

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 896 other followers

%d bloggers like this: