Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8397593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:49:54+00:00 2026-06-09T20:49:54+00:00

I am using a CursorAdapter in a ListFragment to load and display a list

  • 0

I am using a CursorAdapter in a ListFragment to load and display a list of comments.

public class CommentsFragment extends ListFragment implements LoaderCallbacks<Cursor> {

    protected Activity mActivity;
    protected CursorAdapter mAdapter;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mActivity = getActivity();
        mAdapter = new CommentsCursorAdapter(mActivity, null, 0);
        setListAdapter(mAdapter);
        mActivity.getContentResolver().registerContentObserver(CustomContract.Comments.CONTENT_URI, false, new CommentsObserver());
        getLoaderManager().initLoader(0, null, this);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle extras) {
        Uri uri = CustomContract.Comments.CONTENT_URI;
        return new CursorLoader(mActivity, uri, null, null, null, null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        mAdapter.swapCursor(cursor);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        mAdapter.swapCursor(null);
    }

    protected class CommentsObserver extends ContentObserver {

        public CommentsObserver() {
            super(new Handler());
        }

        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            // TODO Trigger a reload.
        }

    }
}

In the associated ContentProvider I added notifyChange() for the insert action.

@Override
public Uri insert(Uri uri, ContentValues values) {
    final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    int match = sUriMatcher.match(uri);
    switch (match) {
        case COMMENTS: {
            long id = db.insert(DatabaseProperties.TABLE_NAME_COMMENTS, null, values);
            Uri itemUri = ContentUris.withAppendedId(uri, id);
            // FIXME Which one is right?
            getContext().getContentResolver().notifyChange(itemUri, null);
            getContext().getContentResolver().notifyChange(uri, null);
            return itemUri;
        }
        default: {
            throw new UnsupportedOperationException("Unknown URI: " + uri);
        }
    }
}

Questions:

  1. Is it okay to pass null to notifyChange() as the observer parameter? If not, what object am I supposed to pass here?
  2. Should I pass the uri or the itemUri in notifyChange()? Why?
  3. What method do I have to call in CommentsObserver#onChange() to update the list of comments?
  4. Is there no interface I could implement with the ListFragment instead of the inner class instance of ContentObserver?
  5. I simply instantiate a new Handler() in the constructor of CommentsObserver. This seems not correct to me – please explain.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:49:55+00:00Added an answer on June 9, 2026 at 8:49 pm

    SqlCursor already have internal ContentObserver and since you’re using this Cursor implementation there is no need for own ContentObserver implementation.

    For easy use you can add:

    class MyContentProvider extends ContentProvider{
      //rest implementation goes here
      @Override
      public Cursor query(Uri uri, String[] projection, String selection,
                String[] selectionArgs, String sortOrder) {
        Cursor cursor = null;
        //check uri, select table name based on uri, etc ...
        cursor = db.query(/*.....*/);
        //add this line:
        cursor.setNotificationUri(getContext().getContentResolver(), uri);
        //before you return cursor
        return cursor;
      }
      @Override
      public Uri insert(Uri uri, ContentValues values) {
         /* ... */
           //you can notify only "dir" uri and is should be enough
           getContext().getContentResolver().notifyChange(uri, null);
           return itemUri;
         /* ... */
      }
      //rest of implementation goes here
    }
    

    after this, CursorLoader internal implementation will take care of reloading/refreshing data …

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using a CursorAdapter to handle my ListActivity : public class Mclass extends
I am using CursorAdapter and below is my adapter class. My list consists of
I'm attempting to create a class that extends a CursorAdapter in order to display
I'm using a database, and there's a list fragment which use a cursor adapter
I am writing an application which is using a SimpleCursorAdapter to display a list
I'm using Novoda ImageLoader (https://github.com/novoda/ImageLoader) and when I use this loader in CursorAdapter everything
I have an Android activity that displays a list of log entries (using a
I've used AsyncTaskLoader to load a cursor from a database query. I followed Android
i have a listview with checkbox in each rows. i'm using custom cursoradapter and
I'm using Android's AutoCompleteTextView with a CursorAdapter to add autocomplete to an app. In

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.