Android SimpleCursorAdapter doesn't update when database changes

后端 未结 6 893
夕颜
夕颜 2020-11-28 04:48

I have an Android ListActivity that is backed by a database Cursor through a SimpleCursorAdapter.

When the items are clicked,

相关标签:
6条回答
  • 2020-11-28 04:51

    It's easy.

    private Db mDbAdapter;
    private Cursor mCursor;
    private SimpleCursorAdapter mCursorAd;
    
    .....................................
    //After removing the item from the DB, use this
    .....................................
    
     mCursor = mDbAdapter.getAllItems();
     mCursorAd.swapCursor(mCursor);
    

    Or use CursorLoader...

    0 讨论(0)
  • 2020-11-28 04:56

    requery is now deprecated. from the documentation:

    This method is deprecated. Don't use this. Just request a new cursor, so you can do this asynchronously and update your list view once the new cursor comes back.

    after obtaining a new cursor one can use theadapter.changeCursor(cursor). this should update the view.

    0 讨论(0)
  • 2020-11-28 05:04

    In case of using loader and automagically generated cursor you can call:

    getLoaderManager().restartLoader(0, null, this);
    

    in your activity, just after changing something on a DB, to regenerate new cursor. Don't forget to also have event handlers defined:

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        CursorLoader cursorLoader =
                new CursorLoader(this,
                        YOUR_URI,
                        YOUR_PROJECTION, null, null, null);
        return cursorLoader;
    }
    
    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        adapter.swapCursor(data);
    }
    
    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        adapter.swapCursor(null);
    }
    
    0 讨论(0)
  • 2020-11-28 05:05

    I am not clear if you set the autoRequery property of CursorAdapter to true.

    The adapter will check the autoRequery property; if it is false, then the cursor will not be changed.

    0 讨论(0)
  • 2020-11-28 05:06

    Call requery() on the Cursor when you change data in the database that you want reflected in that Cursor (or things the Cursor populates, like a ListView via a CursorAdapter).

    A Cursor is akin to an ODBC client-side cursor -- it holds all of the data represented by the query result. Hence, just because you change the data in the database, the Cursor will not know about those changes unless you refresh it via requery().


    UPDATE: This whole question and set of answers should be deleted due to old age, but that's apparently impossible. Anyone seeking Android answers should bear in mind that the Android is a swiftly-moving target, and answers from 2009 are typically worse than are newer answers.

    The current solution is to obtain a fresh Cursor and use either changeCursor() or swapCursor() on the CursorAdapter to affect a data change.

    0 讨论(0)
  • 2020-11-28 05:06

    requery() is already deprecated, just implement the simple updateUI() method like this in your CursorAdapter's child class and call it after data updates:

    private void updateUI(){
        swapCursor(dbHelper.getCursor());
        notifyDataSetChanged();
    }
    
    0 讨论(0)
提交回复
热议问题