Add a SimpleCursorAdapter to a TextView

583 views Asked by At

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

There are 1 answers

0
pskink On BEST ANSWER

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