8

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?.

Naruto
  • 9,476
  • 37
  • 118
  • 201
  • http://stackoverflow.com/questions/8556929/cursoradapter-vs-arrayadapter-for-a-listview please have a look at this –  Jan 15 '14 at 06:05
  • `Please close your question by clicking the checkmark to the left of the answer that helped you most` –  Jan 23 '14 at 09:02
  • Super, done @user3110424, thanks and sorry for delay :) – Naruto Jan 25 '14 at 10:52

2 Answers2

12

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.

Community
  • 1
  • 1
  • do we need to use loader for using cursor adapter? – Naruto Jan 15 '14 at 07:04
  • 1
    if you want you can use Loader with cursor adapter see this [How does CursorLoader with LoaderManager know to send the cursor to a CursorAdapter?](http://stackoverflow.com/questions/11150527/how-does-cursorloader-with-loadermanager-know-to-send-the-cursor-to-a-cursoradap) –  Jan 15 '14 at 07:29
  • Thanks for useful link (+1 for tat), here one thing content provider and loader are depending on each other?, without content provider can i use loader? can you clarify my doubt. thanks – Naruto Jan 15 '14 at 08:46
  • 1
    Its not like that `Loader` depends on `ContentProvider`. You can use `Loader` without `ContentProvider`. If you want to see an implementation you can see this blog in this he hasn't used `ContentProvider` [Android loaders: We meet at last](http://www.techrepublic.com/blog/software-engineer/android-loaders-we-meet-at-last/#.). I think you can get the idea from this. Thanks. :-) –  Jan 15 '14 at 08:57
  • Hi, Thanks. Which one is easy and efficient. as i'm very new to this loaders. Using loader on content provider or just use loader without content provider? – Naruto Jan 15 '14 at 08:59
0

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.

Amit Gupta
  • 8,914
  • 1
  • 25
  • 33
  • What about support to older versions like V2.2, 2.3 versions of phones? – Naruto Jan 15 '14 at 06:29
  • @LLL for working in Lower version like 2.2, 2.3 use android-support-v4.jar. Like Fragment introduced in API-11 but with the help of supporting library its working on lower API as well. – Amit Gupta Jan 15 '14 at 06:32
  • ok, looks cool. Now i have done half of my work with arrayadapters and a basic technics, so this cusors makes my work simple?. its better to use cusors?. – Naruto Jan 15 '14 at 06:50
  • @LLL if you are dealing with database operation Loader is more efficient. As it run in background thread and it handles the orientation change means it wont call the query again, it simply restart the loader. if it makes sense and useful to you then accept my answer. – Amit Gupta Jan 15 '14 at 06:55
  • 1
    Amit, thanks lot. But i need one more suggestion, here i will not share my app API with other apps, so in this case why contentprovider is needed?, this content provider will make work easy or the purpose of content provider is just to expose my app accessible to other 3rd party apps? – Naruto Jan 15 '14 at 07:17
  • 1
    @LLL if you don't want to expose your data to other Application then use android:exported="false" e.g .. – Amit Gupta Jan 15 '14 at 07:22