Android SimpleCursorAdapter doesn't update when database changes

后端 未结 6 913
夕颜
夕颜 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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 onCreateLoader(int id, Bundle args) {
        CursorLoader cursorLoader =
                new CursorLoader(this,
                        YOUR_URI,
                        YOUR_PROJECTION, null, null, null);
        return cursorLoader;
    }
    
    @Override
    public void onLoadFinished(Loader loader, Cursor data) {
        adapter.swapCursor(data);
    }
    
    @Override
    public void onLoaderReset(Loader loader) {
        adapter.swapCursor(null);
    }
    

提交回复
热议问题