Home » Android » android – Better to use Cursor adapter or Array adapter

android – Better to use Cursor adapter or Array adapter

Posted by: admin June 15, 2020 Leave a comment

Questions:

i have around 100’s of schedules stored in database which needs to display them based on Listview based on requirements like, weekly basis, next week, next month, over due schedules etc…

Is it good to load all the schedules on launch of application and show them based on the option user chooses (Weekly, overdue, monthly etc…) in array adapter. Or at run time use the query, fetch the results from DB and use the cusor to load the data on listview using cusoradapter?.

Which method is effeciant?, i feel querying the DB always is expesive operation? is it really true?.

How to&Answers:

In your case CursorAdapter is more appropriate when there is a database because it does not load all the records as ArrayAdapter. It loads only the visible records, or the records you are querying. Here is the documentation for CursorAdapter:

Adapter that exposes data from a Cursor to a ListView widget.

The Cursor must include a column named “_id” or this class will not work. Additionally, using MergeCursor with this class will not work if the merged Cursors have overlapping values in their “_id” columns.

As from the doc of Content provider so it might not useful for you.

You don’t need a provider to use an SQLite database if the use is entirely within your own application.

You can choose CursorAdapter over ArrayAdapter.

Answer:

Better use CursorLoader

A CursorLoader runs an asynchronous query in the background against a ContentProvider, and returns the results to the Activity or FragmentActivity from which it was called. This allows the Activity or FragmentActivity to continue to interact with the user while the query is ongoing.

Loader Introduced in Android 3.0, loaders make it easy to asynchronously load data in an activity or fragment.