In active android there is option to get data from database using CursorLoader, but in example there is only option with 1 table:
MyActivity.this.getSupportLoaderManager().initLoader(0, null, new LoaderCallbacks<Cursor>() {
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle cursor) {
return new CursorLoader(MyActivity.this,
ContentProvider.createUri(TodoItem.class, null),
null, null, null, null
);
}
// ...
});
I want to make join, in normal case i would make uri in content provider for joined tables, but ActiveAndroid delivers ContentProvider, and i dont know how should i do it. Is there option to use custom ContentProvider with ActiveAndroid? Or is there other option?
Related
I'm developing an app based on Google IO presentation architecture using the first approach. Basically I have a Service, ContentProvider backed by SQLite DB and I also use Loaders.
I need a way to update UI when changes to my database occur. For instance a user might want to add an item into his basket. After I insert the item id into the basket table I want to update the UI. What approach should I use? I've seen very little information on ContentObserver so far. Is it the way to go?
In the query method of your ContentProvider attach a listener to the returned cursor:
Cursor cursor = queryBuilder.query(dbConnection, projection, selection, selectionArgs, null, null, sortOrder);
cursor.setNotificationUri(getContext().getContentResolver(), uri);
Then in your insert/update/delete methods use code like this:
final long objectId = dbConnection.insertOrThrow(ObjectTable.TABLE_NAME, null, values);
final Uri newObjectUri = ContentUris.withAppendedId(OBJECT_CONTENT_URI, objectId );
getContext().getContentResolver().notifyChange(newObjectUri , null);
Your CursorLoader will be notified and the OnLoadFinished(Loader, Cursor) will be called again.
If you're not using a Loader, the ContentObserver is the way to go, with a few lines of code you are notified on db changes (but you will need to requery manually).
private ContentObserver objectObserver = new ContentObserver(new Handler()) {
#Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
restartObjectLoader();
}
};
Remember to call in onResume():
getContentResolver().registerContentObserver(ObjectProvider.OBJECT_CONTENT_URI, false, objectObserver);
and in onPause():
getContentResolver().unregisterContentObserver(objectObserver);
Update: UI Changes
This is a larger topic because it depends on the Adapter you use to fill the ListView or RecyclerView.
CursorAdapter
In onLoadFinished(Loader loader, Cursor data)
mAdapter.swapCursor(data);
ArrayAdapter
In onLoadFinished(Loader loader, Cursor data)
Object[] objects = transformCursorToArray(data); //you need to write this method
mAdapter.setObjects(objects); //You need to wrie this method in your implementation on the adapter
mAdapter.notifyDataSetChange();
RecyclerView.Adapter
In onLoadFinished(Loader loader, Cursor data)
Object[] objects = transformCursorToArray(data); //you need to write this method
//Here you have more mAdapter.notify....()
Read from here for different way to notify the RecyclerView.Adapter.
If you are using a list, you can fill adapter again and set it to your list. Or try to inform data set change.
I'm using plain old ListViews, SimpleCursorAdapter, LoaderCallback etc. to read values from a database and display in textViews.
sample code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cateory_list);
mListView = (ListView) findViewById(R.id.list_view);
mAdapter = new SimpleCursorAdapter(
this,
R.layout.category_parent,
null,
new String[] {CategoryTable.COL_2},
new int[] {R.id.text_view},
0);
mListView.setAdapter(mAdapter);
getLoaderManager().initLoader(0, null, this);
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Uri uri = new Uri.Builder()
.scheme("content")
.appendPath(CategoryTable.TB_NAME)
.authority("com.example.auth")
.build();
return new CursorLoader(this, uri, null, null, null, null);
}
Everything works well and the values from database are displayed on the listview. But suppose I want to do some text processing of the values before displaying, how can I do that?
Edit 1: I don't want to do the text processing in main-thread. Is there a way I can use the AsynTaskLoader thread created from CursorLoader and off-load the work over there?
Yep. It is called a ViewBinder. You want a SimpleCursorAdapter.ViewBinder, in your case. It is called just as the data is moved from the adapter, in to the view.
Be careful, though. It is called a lot (likely several times for each view in each cell of the list). It needs to run really fast and, unless you want to drive the GC nuts, should not allocate anything.
I managed to create a Custom Loader by extending CursorLoader and only overriding public Cursor loadInBackground()
This way I'm retrieving the data, do long running text processing, insert the result back to new table and return the cursor of the new table.
Sample code:
#Override
public Cursor loadInBackground() {
Cursor cursor = super.loadInBackground();
cursor.moveToFirst();
try {
ActiveAndroid.beginTransaction();
do {
String s = doProcessing(cursor.getString(colNumber));
createAndInsertIntoNewTable(s);
} while (cursor.moveToNext());
ActiveAndroid.setTransactionSuccessful();
} finally {
ActiveAndroid.endTransaction();
}
return cursorFromNewTable;
}
This completely does the work in AsyncTaskLoader thread and solves my problem perfectly.
I am trying to create a Listview using LoaderCallbacks I am able to see all elements in the list. However now I want to sort it so the latest item in the DB is the first one on top. I have a Coulmn in the db called COLUMN_MESSAGE_DATE which contains a timestamp where I want the latest row to be on top.In the onCreateLoader when I return the loader I sort by this Coulmn but still the latest row is still on the bottom.... Am I missing something?
public class MessageListFragment extends Fragment implements LoaderCallbacks<Cursor>{
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args)
{
// Create the Cursor that will take care of the data being displayed
Log.d("TAG", "onCreateLoader...");
return new CursorLoader(getActivity(),CONTENT_URI, null, null, null,DataBase.COLUMN_MESSAGE_DATE);//I thought this should sort the list by date...
}
#Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor)
{
// Now Bind the data to the View
Log.d("TAG", "onLoadFinished...ARG1= " + cursor);
mCusrorAdapter = new CustomCursorAdapter(getActivity(), cursor);
mListView.setAdapter(mCusrorAdapter);
}
}
Although I cannot be sure without seeing your data, a timestamp is usually an incrementing value. That is, a current timestamp has a greater value than an older one. The default sort order is ASCending. Here I believe you want DESCending:
return new CursorLoader(getActivity(),CONTENT_URI, null, null, null,DataBase.COLUMN_MESSAGE_DATE + " DESC");
I have tried for hours but couldn't achieve this. I tried sending via Bundle, via static variables, via if statement but nothing worked.
I have a situation where I want my Loader to load different sets of data once user has clicked a menu item. This means the query should be changed once it has received the notifyChange. The code is pretty standard Loader code like this:
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String query = args.getString("query");
String[] projection = { DbCreate.ID, DbCreate.COL_ITEM, DbCreate.COL_STATUS, DbCreate.COL_SYNCED,
DbCreate.COL_PRIORITY, DbCreate.COL_COMPLETEBY, DbCreate.COL_TAG, DbCreate.COL_ASSIGNED, DbCreate.COL_SHARED};
CursorLoader cursorLoader = new CursorLoader(getActivity(),
DbProvider.CONTENT_URI_DATA, projection, query, null, "priority DESC, status ASC, _id DESC");
return cursorLoader;
}
I tried usual if(...) statement inside this onCreate method too but it doesn't work. This means the notifyChange just triggers the already created object. So how can I inject a new 'query' value on the notifyChange?
I am doing same thing in my code you need to restart cursor loader again when you want to change the data notify works on adapter not on cursor loader
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle arg1) {
// This is called when a new Loader needs to be created.
// Now create and return a CursorLoader that will take care of
// creating a Cursor for the data being displayed.
Uri contentUri = MyContentProvider.CONTENT_URI_FACEBOOK;
switch (id) {
case FACEBOOK:
contentUri = MyContentProvider.CONTENT_URI_FACEBOOK;
break;
case LINKEDIN:
contentUri = MyContentProvider.CONTENT_URI_LINKEDIN;
break;
}
return new CursorLoader(getActivity(), contentUri, null, null, null,
null);
}
for various queries I am doing like this only
getLoaderManager().restartLoader(FACEBOOK, null, Profile.this);
getLoaderManager().restartLoader(LINKEDIN, null, Profile.this);
so you just need to restart loader
If I understand your problem correctly, you probably don't want to worry about trying to change the dataset via notifyChange. I think you most likely want to simply fire-off a new loader (or do a forced reload on the existing one). Let the loader set the adapter on load completion and that will cause a notify on the data set.
I.E. User clicks menu, and you fire the loader with the new query parameters/other info. If the loader is already running, you can cancel it and start a new one etc.
I continue to struggle with getting a query to work with a CursorLoader in a ListFragment. I suspect part of my problem is that I'm unsure about certain details. I have an xml file, myfragment.xml, which defines the two fragments in my app. The first fragment, my ListFragment, is identified by:
android:id="#+id/frag_mylist"
When I call SimpleCursorAdapter in my ListFragment class, I believe I should do this:
String[] dataColumns = { "fieldname", "_id" };
int[] viewIDs = { R.id.frag_mylist };
mAdapter = new SimpleCursorAdapter(getActivity(), R.layout.myfragment, null, dataColumns, viewIDs, 0);
setListAdapter(mAdapter);
getLoaderManager().initLoader(0, info, (LoaderCallbacks<Cursor>) this);
where info is a Bundle that I've passed from a previous activity. Is that right? Also, I've seen some examples with 0 as the last parameter for SimpleCursorAdapter, others with CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER. What's the difference? Finally, this page may indicate that I have to retrieve a LoaderManager in my code like so:
private LoaderManager mLoaderManager;
public void onCreate(savedInstanceState) {
super.onCreate(savedInstanceState);
mLoaderManager = this.getSupportLoaderManager();
}
but this is the only place I've seen this. Is this necessary? I'm hoping that getting answers to these questions will help me dig down to why my query is returning no results. I'm fairly confident that my database is being created and populated at this point. Thanks much!
As requested below, here are the three methods of my LoaderManager.LoaderCallbacks interface:
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String selection = "level='" + args.getString("Level") + "'";
return (Loader<Cursor>) new CursorLoader(getActivity(), MY_URI,
PROJECTION, selection, null, null);
}
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
switch (loader.getId()) {
case LOADER_ID:
mAdapter.swapCursor((android.database.Cursor) cursor);
break;
}
}
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
Let me add that I've verified through the debugger that args.GetString("Level") in the onCreateLoader method is "Beginning", which is what it should be.
Add this line within your onLoadFinished after you swap the cursor
mAdapter.notifyDataSetChanged()