Update SimpleCursorAdapter while maintaining scroll position in ListView

前端 未结 2 1222
伪装坚强ぢ
伪装坚强ぢ 2021-01-02 08:22

My problem: My ListView resets its scroll position to the top whenever I update its contents through its (customized) SimpleCursorAdapter. I would like the ListView to main

相关标签:
2条回答
  • 2021-01-02 08:57

    Did the same in post execute of Async Task used following code worked for me

     Cursor cursor = mDataHelper.getWritableDatabase().query(
                            DataContract.Incares.TABLE_NAME,  // Table to Query
                            null, // all columns
                            null, // Columns for the "where" clause
                            null, // Values for the "where" clause
                            null, // columns to group by
                            null, // columns to filter by row groups
                            DataContract.Incares.UUID +" DESC" // sort order
                    );
    CursorAdapter.swapCursor(cursor);
    CursorAdapter.notifyDataSetChanged();
    

    with CursorAdapter being a global variable

    0 讨论(0)
  • 2021-01-02 09:09

    I've had a similar problem: My CursorAdapter reset the scroll position whenever the contents of the cursor changed.

    I didn't realize that I actually did set a new list adapter each time the data changed. I thought the cursor himself would notify the adapter about changes to it's content but in my case it is the ContentProvider that triggers LoaderManager.LoaderCallbacks.onLoadFinished() in my ListFragment. In that callback I now use CursorAdapter.changeCursor() instead of creating a new adapter and everything works just fine.

    I hope this helps solving your problem.

    0 讨论(0)
提交回复
热议问题