An Analysis between BaseAdapters and CursorAdapters
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 https://thinkandroid.wordpress.com/category/android-tutorials/cursoradapter-tutorials/
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.