Comparing CursorAdapters and BaseAdapters

So under what typical scenarios would you find yourself using a BaseAdapter instead of a CursorAdapter and vice versa? We've already thought of a few instances previously, but let's take a little more time to brainstorm some use cases, just to get you even more comfortable with the two ListAdapters and when to switch between the two.

The general rule of thumb should be whenever your underlying data is returned as a Cursor, use a CursorAdapter, and whenever your data is returned or can be manipulated into a list of objects, use a BaseAdapter.

This means that for most network requests when the data is returned as one long String (again, getting a little ahead of myself but this String will typically be in either an XML or JSON format), it's best to simply parse the String and convert it into objects. These can then be stored in a list and passed into a custom BaseAdapter. This will often also be the case if you are calling an external API, in which case the data will typically come back as either XML or JSON. The exception then is when you want to cache the results.

Caching typically involves temporarily storing some data in a more local (or faster) area of memory (with CPU systems, this means storing data in RAM instead of on disk, and for mobile applications this means storing data locally instead of continuously requesting external data through a network). If you want to cache some of your network calls – whether it's for performance reasons or for offline access reasons – then the suggested flow is to make your network request, retrieve the formatted data String, parse the data String, and insert the data into a SQLite database (meant to mimic the external database). Then, since your data is already housed in a SQLite database, it's best (and easiest) to just make a quick query and get back a Cursor.

Now, what about a scenario where you have a static list of primitive objects, for instance Strings? This would often be the case if you had some kind of fixed table of contents where the user has to select from a pre-defined list of options. In that case, both a BaseAdapter and a CursorAdapter would be overkill, and instead you should opt to use a much simpler kind of Adapter known as the ArrayAdapter. I tried not to spend any time on this kind of ListAdapter, as it's extremely simple to use and conceptually it's extremely simple as well – if you have a static Array of Strings and you want to make a list out of them, just pass that Array into an ArrayAdapter and you're good to go.

However, this is all I will say on the ArrayAdapter and I invite you to read through the example found on the following site:

http://developer.android.com/resources/tutorials/views/hello-listview.html

Otherwise, just remember that for lightweight static data, use the ArrayAdapter, for dynamic object-oriented data, use the BaseAdapter and for locally stored subtable based data, use the CursorAdapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.66.156