Вопрос

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);
}
Это было полезно?

Решение

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top