android - CursorLoader & SQLite without Content Provider

前端 未结 2 1814
旧巷少年郎
旧巷少年郎 2020-12-04 07:09

I know this has been discussed yet I wanted to ask about the current state of the matter. Do i have to create a ContentProvider to use CursorLoader in connection with a sqli

2条回答
  •  忘掉有多难
    2020-12-04 07:49

    Here is my solution, in my onCreateLoader

    {
    Uri u = Uri.parse("content://what_string_you_want");
    return new CursorLoader(this, yourURI, projection, null, null, null) {
        private final ForceLoadContentObserver mObserver = new ForceLoadContentObserver();
    
        @Override
        public Cursor loadInBackground() {
            Cursor c = YOUR_DATABASE.doYourQuery(...);
            if (c != null) {
              // Ensure the cursor window is filled
               c.getCount();
               c.registerContentObserver(mObserver);
            }
    
            c.setNotificationUri(getContext().getContentResolver(), getUri());
            return c;
        }
    };
    }
    

    After the code that will change DB, add

     getContentResolver().notifyChange(
                Uri.parse("content://same_with_first_string"), null);
    

提交回复
热议问题