Reputation: 3234
I want to know what is the difference between Adapter and Loader in Android. I have already looked up at the documentation but can't figure out the difference between them. Any help would be appreciated. Thanks!
Upvotes: 18
Views: 9357
Reputation: 8225
Both provide an abstraction for data access, but the Loader performs the query in the background whereas an Adapter executes in the current (presumably UI) thread.
For example, a straightforward way to access a Content Provider is with a SimpleCursorAdapter. But querying large amounts of data directly from an Activity may cause it to become blocked resulting in an "Application Not Responding" message. Even if it doesn't, users will see an annoying delay in the UI. To avoid these problems, you should initiate a query on a separate thread, wait for it to finish, and then display the results. This is what the CursorLoader will do.
That being said, they are sometimes used in conjunction with one another. In this example data is first loaded with a CursorLoader and then that cursor is updated in an Adapter of an AdapterView for display.
Upvotes: 24
Reputation: 32041
I think these two classes operate on a different level of abstraction: While the Adapter is a interface which needs to be implemented by a class providing the actual data, the Loader contains functionality to asynchronously load data based on a Cursor.
I think you can think of a Loader as a Adapter plus a AsyncTask running it.
Also keep in mind that the Loader is only available from Android 3.0 on.
Upvotes: 3