I need to make a ListAdapter that presents data from multiple ContentProviders. The ContentProviders themselves represent one table each from relational database.
I want to use the CursorLoader system to retrieve aggregate data into ListView. Is this possible to do with 1 loader or do I need to use multiple loaders? I'd prefer to use one.
I'm not sure how I can have 2 ContentProviders interact with each other beyond doing the join manually in code which doesn't seem like a great option either.
A content provider manages access to a central repository of data. A provider is part of an Android application, which often provides its own UI for working with the data. However, content providers are primarily intended to be used by other applications, which access the provider using a provider client object.
Create Content Provider Next you will need to create your own database to keep the content. Usually, Android uses SQLite database and framework needs to override onCreate() method which will use SQLite Open Helper method to create or open the provider's database.
What is the Content Resolver? The Content Resolver is the single, global instance in your application that provides access to your (and other applications') content providers.
You will have to write a Custom Loader class. For example:
public class FooLoader extends AsyncTaskLoader {
Context context;
public FooLoader(Context context) {
super(context);
this.context = context;
}
@Override
public Cursor loadInBackground() {
Log.d(TAG, "loadInBackground");
YourDatabase dbHelper = new YourDataBase(context);
SQLiteDatabase db= dbHelper.getReadableDatabase();
/*** create a custom cursor whether it is join of multiple tables or complex query**/
Cursor cursor = db.query(<TableName>, null,null, null, null, null, null, null);
return cursor;
}
}
In the calling activity or fragments onCreate()
method, you would need to call the custom loader class:
public class MyFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate():" + mContent);
Loader loader = getLoaderManager().initLoader(0, null, this);
loader.forceLoad();
}
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
Log.d(TAG, "onCreateLoader()") ;
return new FooLoader(getActivity());
}
@Override
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
Log.d(TAG, "onLoadFinished");
}
@Override
public void onLoaderReset(Loader<Cursor> cursorLoader) {
}
}
You might want to take a look at CursorJoiner.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With