I have a simple contentProvider, a layout with a ListView and a button for adding Items in content Provider and a CursorLoader. The android.content.Loader, D reference states that
The Loader will monitor for changes to the data, and report them to
you through new calls here. You should not monitor the data yourself.
For example, if the data is a Cursor and you place it in a
CursorAdapter, use the CursorAdapter(android.content.Context,
android.database.Cursor, int) constructor without passing in either
FLAG_AUTO_REQUERY or FLAG_REGISTER_CONTENT_OBSERVER (that is, use 0
for the flags argument). This prevents the CursorAdapter from doing
its own observing of the Cursor, which is not needed since when a
change happens you will get a new Cursor throw another call here.
But the Log.info line in the onLoadFinished method was not executed and listView didn't refreshed. Here is my (simple) code:
public class HomeActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor>{
static final String TAG = "HomeActivity";
SimpleCursorAdapter adapter;
ListView listAnnunciVicini;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
listAnnunciVicini = (ListView) findViewById(R.id.lista_annunci_vicini);
adapter = new SimpleCursorAdapter(this, R.layout.list_item, null,
new String[] {
ContentDescriptor.Annunci.Cols.ID,
ContentDescriptor.Annunci.Cols.TITOLO,
ContentDescriptor.Annunci.Cols.DESCRIZIONE
}, new int[] {
R.id.list_annunci_item_id_annuncio,
R.id.list_annunci_item_titolo_annuncio,
R.id.list_annunci_item_descrizione_annuncio
}, 0);
listAnnunciVicini.setAdapter(adapter);
// Prepare the loader. Either re-connect with an existing one,
// or start a new one.
getSupportLoaderManager().initLoader(0, null, this).forceLoad();
}
public void addRandomItem(View sender) {
ContentValues dataToAdd = new ContentValues();
dataToAdd.put(ContentDescriptor.Annunci.Cols.TITOLO, "Titolo");
dataToAdd.put(ContentDescriptor.Annunci.Cols.DESCRIZIONE, "Lorem ipsum dolor sit amet.");
this.getContentResolver().insert(ContentDescriptor.Annunci.CONTENT_URI, dataToAdd);
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// creating a Cursor for the data being displayed.
String[] proiezione = new String[] {ContentDescriptor.Annunci.Cols.ID, ContentDescriptor.Annunci.Cols.TITOLO, ContentDescriptor.Annunci.Cols.DESCRIZIONE };
CursorLoader cl = new CursorLoader(this, ContentDescriptor.Annunci.CONTENT_URI, proiezione, null, null, null);
return cl;
}
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// Swap the new cursor in. (The framework will take care of closing the
// old cursor once we return.)
adapter.swapCursor(data);
Log.i(TAG, "I dati sono stati ricaricati");
}
public void onLoaderReset(Loader<Cursor> loader) {
// This is called when the last Cursor provided to onLoadFinished()
// above is about to be closed. We need to make sure we are no
// longer using it.
adapter.swapCursor(null);
}
}
Any suggestion?
AFAIK, you need to implement notification yourself in ContentProvider. For this, add something like getContext().getContentResolver().notifyChange(uri, null); to insert,update and delete method of ContentProvider and invoke.setNotificationUri(getContext().getContentResolver(), uri) on your Cursor in queryas described in official documentation. Here you can find the whole picture.
Note: You should not close the cursor (cursor.close()) in order to get
notifications about the changes.
Related
I am building a very simple app that contains a SQLiteDatabase which I want to display in a ListFragment, using a custom SimpleCursorAdapter.
My code is working fine, but I'm not sure if I'm doing things the correct way. I have searched a lot for (authoritative) examples of this, but have only found either overly simplified examples using ArrayAdapter, or overly complicated examples using ContentProvider.
ListFragment
public class CallListFragment extends ListFragment{
private CallListDbHelper dbHelper;
private SQLiteDatabase db;
private Cursor cursor;
private CallListAdapter adapter;
#Override
public void onResume() {
super.onResume();
// Create a database helper
dbHelper = new CallListDbHelper(getActivity());
// Get the database
db = dbHelper.getReadableDatabase();
// Get a cursor to the entire call list from the database
cursor = db.query( // SELECT
CallEntry.TABLE_NAME, // FROM ...
new String[] { // <columns>
CallEntry._ID,
CallEntry.COLUMN_NUMBER,
CallEntry.COLUMN_TIME },
null, // WHERE ... (x = ?, y = ?)
null, // <columnX, columnY>
null, // GROUP BY ...
null, // HAVING ...
CallEntry.COLUMN_TIME + " DESC" // ORDER BY ...
);
adapter = new CallListAdapter(getActivity(), cursor);
setListAdapter(adapter);
}
#Override
public void onPause() {
super.onPause();
// Close cursor, database and helper
if( null !=cursor ) cursor.close();
if( null != db ) db.close();
if( null != dbHelper ) dbHelper.close();
}
}
Adapter
public class CallListAdapter extends SimpleCursorAdapter {
private static final String[] FROM = {
CallListContract.CallEntry.COLUMN_NUMBER,
CallListContract.CallEntry.COLUMN_TIME
};
private static final int[] TO = {
R.id.phoneNumber,
R.id.time
};
public CallListAdapter(Context context, Cursor cursor){
this(context, R.layout.listitem_call, cursor, FROM, TO);
}
private CallListAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to) {
super(context, layout, cursor, from, to, 0);
}
}
For something simple it will work. But...
Do you use your DB in one place?
How big is your DB?
How often do you hit onResume/onPause?
General recommendation is onResume/onPause should be as fast as possible, but DB operations can be blocking...
Especially first touch (creation) of DB can be time-consuming and potentially you can get ANR.
Don't use Activity as a Context or you may can get memory leaks. The recommended way is to use the Context of your Application in conjunction with singleton pattern, so you'll not bother to close DB.
CursorLoader needs the cursor to be open in order to function and will call close() on the cursor for you. As for SimpleCursorAdapter I don't see auto-close feature in the source code =(.
I'm developing an application which relies on database (local) - and this database updates frequently.
What I'm trying to achieve is:
Suppose the data is shown in a listview. I want the listview to update the dataset as soon as any change in the database happens (or a specific table to be precise).
So far I've thought of these options:
SQLiteOpenHelper class: whenever an update/insert is done it'll notify the activity to update listview via BroadcastReceiver.
ContentProvider with CursorLoader (haven't used it before so a little skeptical)
Something else? Please suggest.
Which is the best way to achieve consistent and immediate updates without blocking the UI (performance)?
As suggested by #Karakuri created a custom CursorLoader by extending CursorLoader class without ContentProvider.
Here's the solution:
CustomCursorLoader.class
public class CustomCursorLoader extends CursorLoader {
private final ForceLoadContentObserver forceLoadContentObserver = new ForceLoadContentObserver();
public CustomCursorLoader(Context context, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
super(context, uri, projection, selection, selectionArgs, sortOrder);
}
#Override
public Cursor loadInBackground() {
Cursor cursor = /* get cursor from DBHandler class */;
cursor.setNotificationUri(getContext().getContentResolver(), CONTENT_URI);
if (cursor != null) {
cursor.getCount();
cursor.registerContentObserver(forceLoadContentObserver);
}
return cursor;
}
}
Every time you make a change to DB, do:
getContentResolver().notifyChange(CONTENT_URI, null);
In Activity class:
implement interface LoaderManager.LoaderCallbacks<Cursor>
initiate loader getLoaderManager().initLoader(0, null, this);
and override these methods:
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CustomCursorLoader(this, CONTENT_URI, null, null, null, null);
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
customCursorLoaderAdapter.swapCursor(data);
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
customCursorLoaderAdapter.swapCursor(null);
}
extend CursorAdapter class to create listview adapter and you're done.
If performance is crucial for your app you should take a look on Realm for Android database. It provides better efficiency than SQLite and you can use RealmChangeListener to listen for changes in the database.
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 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()
So I have my MainDisplayActivity which implements both Activity and LoaderManager.LoaderCallbacks<Cursor>. Here I have A ListView, which I fill with Agenda information that I get from my database using a ContentProvider. I also have a GridView which is a calendar. I have it set up when clicking a cell, that the agenda is updated with the day clicked. My problem is that when reusing the Loader I created in onCreate() inside of the setOnItemClickListener(), it does not refresh the information with the new cursor I am creating. I can just create a new Loader with another ID, and it works once, but once I click another day, it stops refreshing. The problem, lies in the cursor. How can I refresh a cursor from a Loader so I don't have to keep creating a new Loader? Thanks in advance!
Initial call to create the Loader in onCreate() in My MainDisplayActivity class:
makeProviderBundle(new String[] {"_id, event_name, start_date, start_time, end_date, end_time, location"},
"date(?) >= start_date and date(?) <= end_date", new String[]{getChosenDate(), getChosenDate()}, null);
getLoaderManager().initLoader(0, myBundle, MainDisplayActivity.this);
list.setAdapter(agendaAdapter);
These are the overridden methods from LoaderCallbacks<Cursor>
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Uri baseUri = SmartCalProvider.CONTENT_URI;
return new CursorLoader(this, baseUri, args.getStringArray("projection"),
args.getString("selection"), args.getStringArray("selectionArgs"), args.getBoolean("sortOrder") ? args.getString("sortOrder") : null );
}
#Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
agendaAdapter.swapCursor(arg1);
}
#Override
public void onLoaderReset(Loader<Cursor> arg0) {
//Not really sure how this works. Maybe I need to fix this here?
agendaAdapter.swapCursor(null);
}
public void makeProviderBundle(String[] projection, String selection, String[] selectionArgs, String sortOrder){
/*this is a convenience method to pass it arguments
* to pass into myBundle which in turn is passed
* into the Cursor loader to query the smartcal.db*/
myBundle = new Bundle();
myBundle.putStringArray("projection", projection);
myBundle.putString("selection", selection);
myBundle.putStringArray("selectionArgs", selectionArgs);
if(sortOrder != null) myBundle.putString("sortOrder", sortOrder);
}
Any additional code needed please do not hesitate to ask. Thanks again for any help!
You just need to move your code around a little and call restartLoader. That is,
When the user clicks a list item, you somehow change the private instance variable that is returned in getChosenDate() (I am being intentionally vague here because you didn't specify what exactly getChosenDate() returns).
Once the change is made, call getLoaderManager().restartLoader() on your Loader. Your old data will be discarded and restartLoader will trigger onCreateLoader to be called again. This time around, getChosenDate() will return a different value (depending on the list item that was clicked) and that should do it. Your onCreateLoader implementation should look something like this:
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Uri baseUri = SmartCalProvider.CONTENT_URI;
makeProviderBundle(
new String[] { "_id, event_name, start_date, start_time, end_date, end_time, location" },
"date(?) >= start_date and date(?) <= end_date",
new String[]{ getChosenDate(), getChosenDate() },
null);
return new CursorLoader(
this,
baseUri,
args.getStringArray("projection"),
args.getString("selection"),
args.getStringArray("selectionArgs"),
args.getBoolean("sortOrder") ? args.getString("sortOrder") : null);
}
Let me know if that makes sense. :)