CursorLoader usage without ContentProvider

前端 未结 5 566

Android SDK documentation says that startManagingCursor() method is depracated:

This method is deprecated. Use the new CursorLoader class

5条回答
  •  清酒与你
    2020-11-22 13:27

    A third option would be to simply override loadInBackground:

    public class CustomCursorLoader extends CursorLoader {
        private final ForceLoadContentObserver mObserver = new ForceLoadContentObserver();
    
        @Override
        public Cursor loadInBackground() {
            Cursor cursor = ... // get your cursor from wherever you like
    
            if (cursor != null) {
                // Ensure the cursor window is filled
                cursor.getCount();
                cursor.registerContentObserver(mObserver);
            }
    
            return cursor;
        }
    };
    

    This will also take care of re-querying your cursor when the database changes.

    Only caveat: You'll have to define another observer, since Google in it's infinite wisdom decided to make theirs package private. If you put the class into the same package as the original one (or the compat one) you can actually use the original observer. The observer is a very lightweight object and isn't used anywhere else, so this doesn't make much of a difference.

提交回复
热议问题