0
votes

Is there a way to use a SimpleCursorAdapter via a CursorLoader so that I can keep a TextView up to date with my database changes? I have an account balance that I want to maintain without having to manage every change myself. My TextView isn't in anything that has an adapter. It is more like a banner. The problem with the code below is that setViewValue is never called which I'm assuming is that The adapter hasn't been set to anything. Any help or suggestions would be appreciated. Thanks.

        mAdapter = new SimpleCursorAdapter(this, 
            R.layout.todays_bal_header, 
            null,
            new String[] { TransactionTable.TRANS_AMOUNT, TransactionTable.TRANS_ID },
            new int[] { R.id.todays_balance }, 
            0 );

    mAdapter.setViewBinder(new ViewBinder() {
        public boolean setViewValue(View aView, Cursor c, int aColumnIndex) {

            if (aView.getId() == R.id.todays_balance) {
                                    //Calculate value
                TextView textView = (TextView) aView;
                textView.setText(Integer.toString(value));
                return true;
            }

            return false;
        }
    });
    getSupportLoaderManager().initLoader(URL_LOADER, null, this);

Here are my callbacks

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    mAdapter.changeCursor(cursor);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
    mAdapter.changeCursor(null);
}
1
use ContentResolver.registerContentObserver(Uri, boolean, ContentObserver) and notifyChange(Uri, ContentObserve)pskink
I think this is a great alternative. If you want to add it as a comment I can accept it. I went with this solution.DroidT
btw when using CursorLoader you dont have to do anything in your activity code: onLoadFinished is being called each time the Cursor changespskink

1 Answers

1
votes

Use ContentResolver.registerContentObserver(Uri, boolean, ContentObserver) in your code (Activity/View) and ContentResolver.notifyChange(Uri, ContentObserver) in a ContentProvider.