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

  • SEARCH
  • Home
  • 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 8729127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:46:43+00:00 2026-06-13T08:46:43+00:00

I have an SQLiteDatabase whose data is managed by a Content Provider. I’m using

  • 0

I have an SQLiteDatabase whose data is managed by a Content Provider. I’m using a tabbed layout. The first tab has the ability to add rows to the database, whereas the second tab shows items from the database. As I add items to the database from the first tab, the changes should be reflected when I move to the other tab.

Data is being added to the database correctly, and upon first opening of the app, all the current data (and anything new added in a previous version of the app) will appear. However, adding new items to the database is not reflected in the ListFragment.

@Override
    public void onClick(View v) {
        if(v == addSale) {
            Item item = new Item(rn(), null, 100);
            data.add(item);
            total += item.getAmount();
        } else if(v == save) {
            for(Item i: data) {
                ContentValues cv = new ContentValues();
                cv.put(DatabaseProvider.COLUMN_COST, i.getAmount());
                cv.put(DatabaseProvider.COLUMN_ITEM, i.getItem());
                cv.put(DatabaseProvider.COLUMN_PERSON, i.getPerson());
                this.getActivity().getContentResolver().insert(DatabaseProvider.CONTENT_URI, cv);
            }
            total = 0;
            data.clear();
        } else if(v == clear) {
            data.clear();
            total = 0;
        }
        items.notifyDataSetChanged();
        totalView.setText(Item.format(total));
    }

Here is where I add the items to the database specifically with these lines:

ContentValues cv = new ContentValues();
cv.put(DatabaseProvider.COLUMN_COST, i.getAmount());
cv.put(DatabaseProvider.COLUMN_ITEM, i.getItem());
cv.put(DatabaseProvider.COLUMN_PERSON, i.getPerson());
this.getActivity().getContentResolver().insert(DatabaseProvider.CONTENT_URI, cv);

As I said earlier, items are put into the database correctly, so I’m reasonably sure that this is correct.

Here is the insert method of my DatabaseProvider

@Override
public Uri insert(Uri uri, ContentValues initialValues) {
    if (sUriMatcher.match(uri) != TABLE) {
        throw new IllegalArgumentException("Invalid URI: " + uri);
    }

    if (initialValues == null) {
        initialValues = new ContentValues();
    }

    SQLiteDatabase db = dbHelper.getWritableDatabase();

    long rowId = db.insert(TABLE_SALES, COLUMN_COST, initialValues);

    if (rowId > 0) {
        Uri newUri = ContentUris.withAppendedId(CONTENT_URI, rowId);
        getContext().getContentResolver().notifyChange(uri, null);
        return newUri;
    }

    throw new SQLException("Failed to insert row into: " + uri);
}

From the various tutorials and other SO questions, it seems as if

getContext().getContentResolver().notifyChange(uri, null);

is the key to getting it to update, and it’s there, and is called. But nothing updates.

Finally, my list fragment that display all of the data.

package org.worldsproject.android.barcode;

import org.worldsproject.android.barcode.database.DatabaseProvider;

import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;

import com.actionbarsherlock.app.SherlockListFragment;

public class RunningTotal extends SherlockListFragment implements
        LoaderManager.LoaderCallbacks<Cursor> {
    public static final String TAG = "Running Total";

    public static final int RUNNING_TOTAL_ID = 1;
    private SimpleCursorAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String[] uiBindFrom = { DatabaseProvider.COLUMN_PERSON };
        int[] uiBindTo = { R.id.titled };

        adapter = new SimpleCursorAdapter(
                getActivity().getApplicationContext(), R.layout.list_title,
                null, uiBindFrom, uiBindTo,
                CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

        setListAdapter(adapter);
        getLoaderManager().initLoader(RUNNING_TOTAL_ID, null, this);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
        String[] projection = { DatabaseProvider.COLUMN_ID,
                DatabaseProvider.COLUMN_PERSON};
        return new CursorLoader(getActivity(), DatabaseProvider.CONTENT_URI,
                projection, null, null, null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {
        // TODO Auto-generated method stub
        adapter.swapCursor(cursor);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> arg0) {
        // TODO Auto-generated method stub
        adapter.swapCursor(null);
    }

}

It’s nearly a direct clone of http://mobile.tutsplus.com/tutorials/android/android-sdk_loading-data_cursorloader/ and at the moment just displays the name column of each row in the database. It shows previous entries, but as I’ve said, doesn’t update. What step am I missing to get it to update?

  • 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-13T08:46:44+00:00Added an answer on June 13, 2026 at 8:46 am

    I think your

    getContext().getContentResolver().notifyChange(uri, null);
    

    may be fine.

    I can’t see your query function for your DatabaseProvider, but did you remember to set the notificationUri for the cursor you are returning in your query function?

    In your query() function of the DatabaseProvider, you should set the notification Uri for the cursor, or else the cursor won’t get a notification even if you do a notifyChange():

    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
       // blah blah
       cursor.setNotificationUri(getContext().getContentResolver(), uri);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Android app that has a Content Provider with an SQLiteDatabase implementation.
I have an sqlite database whose data I need to transfer over the network,
I have a table login2 in /data/data/sankalp.jain.shre/databases/loginfinal.db. The database has been created correctly which
I have a SQLiteDatabase data member that I initialize in onCreate and call .close()
I have two instances of SQLiteDatabase database. And I need to copy data from
I have a problem using sqlite_last_insert_rowid in PHP. CODE: $database = new SQLiteDatabase('example.db'); $sql_1
I have an SQLite database, eventually will be a MySQL database and I'm using
I have a database where I store two different kinds of data. One table
I have a text file which contains data I need to preload into a
I have a class DatabaseHandler. I have overridden the onCreate(SQLiteDatabase db) method with the

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.