androidandroid-contentprovidermergecursor

MergeCursor not updating after data change


I'm creating a MergeCursor like so:

    @Override
    public Cursor loadInBackground() {
        Log.d(LOG, "loading data in background");

        ContentResolver cr = getContext().getContentResolver();
        Cursor people = getContext().getContentResolver().query(PeopleProvider.CONTENT_URI, null, null, null, null);
        people.setNotificationUri(cr, PeopleProvider.CONTENT_URI);
        Cursor jobs = getContext().getContentResolver().query(JobProvider.CONTENT_URI, null, null, null, null);
        jobs.setNotificationUri(cr, JobsProvider.CONTENT_URI);
        Cursor horses = getContext().getContentResolver().query(HorseProvider.CONTENT_URI, null, null, null, null);
        horses.setNotificationUri(cr, HorseProvider.CONTENT_URI);

        Cursor[] c = new Cursor[3];

        c[0] = people;
        c[1] = jobs;
        c[2] = horses;

        MergeCursor mc = new MergeCursor(c);

        return mc;
    }

Then when I add new rows to the corresponding db tables:

    "PeopleProvider.java"

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        Log.d(LOG, "Insert into People");
        long id = database.insertOrThrow(DatabaseConstants.TABLE_PEOPLE, null, values);
        getContext().getContentResolver().notifyChange(uri, null);
        return ContentUris.withAppendedId(uri, id);
    }

I also tried:

    "WhereDataIsPresentedFragment.java"
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d(LOG, "onActivityResult()");
        switch(requestCode) {
        case NEW_PEOPLE_ACTIVITY:
            Log.d(LOG, "Returned with newly added person");
            if (resultCode==Activity.RESULT_OK) {
                mListViewAdapter.notifyDataSetChanged();
                getActivity().getContentResolver().notifyChange(PeopleProvider.CONTENT_URI, null);
            }
            break;
        }
    }

All methods are being called (confirmed via Log.d), so I'm not sure why the cursors are not being updated when the notifcations are being fired. Any insight into making this work would be greatly appreciated. I've had no issues with regular Cursors in the past, but no luck with this MergeCursor business.


Solution

  • So the way I got this working (whether it's right or not) is by further subclassing my AsyncTaskLoader class:

        @Override
        protected void onStartLoading() {
    
            forceLoad();
        }
    

    Essentially by adding the forceLoad(); to the onStartLoading() method the everything now works as expected. I was able to remove all the additional calls to notifyDataSetChanged() and notifications to URIs. That single line fixed everything. If someone has a better solution please do let me know, but for now this is what I got.