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 8464087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:38:08+00:00 2026-06-10T14:38:08+00:00

In my application I have a content provider which uses a database for the

  • 0

In my application I have a content provider which uses a database for the content provided. When the database is created the first time it needs to be filled from the raw content of a Json file. My idea was that i trigger this filling of the database at onCreate of my SQLiteOpenHelper subclass. This works fine yet I am not sure how to handle the the communication between application and content provider when the app is running the first time. Basically i would like to show some sort of a splash screen while the database is filled. Yet how does the application get informed that

  1. the content provider is busy filling the database when running the first time
  2. the content provider is ready to go

Surely I could fill the database from the application by calling the content provider with each dataset yet I would prefer doing it within the sphere of the content provider so that the application does not have to handle the reading of the json file etc. Besides design preferences it would also enable the content provider to fill the database more efficiently because it would have the whole dataset at once. I have a feeling this is not possible yet I hope I miss some simple point.

Any suggestions how to achieve this would be highly appreciated.

Thanks

martin

  • 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-10T14:38:09+00:00Added an answer on June 10, 2026 at 2:38 pm

    When using a content Provider i would presume that your using a DBHelper class to manage the creation of the database. Below is the code from the android notes example project.

    This shows how the DBHelper constructor is intelligent enough to determine if the database has been created before. In the createDatabase method i would subsequently call a method to pre-populate the database, from as you say a json file.

    The problem is that this doesn’t really allow you to communicate to the Activity that your database hasn’t been initialised.

    One thought could be that you use SharedPreferences to store the fact you’ve populated the database. You could then check the sharedPreference in the activity on startup, Call the content provider to populate the database and then store in the shared preference that you’ve done this task already.

    Just be aware that i’m not sure if the sharedPreferences maintain the same state as the database if you for example erase the data from the android settings menu. You’d need to check that.

    http://code.google.com/p/android-notes/source/browse/trunk/src/com/bitsetters/android/notes/DBHelper.java?r=10

    public class DBHelper {
    
    private static final String DATABASE_NAME = "notes";
    private static final String TABLE_DBVERSION = "dbversion";
    private static final String TABLE_NOTES = "notes";
    private static final int DATABASE_VERSION = 1;
    private static String TAG = "DBHelper";
    Context myCtx;
    
    private static final String DBVERSION_CREATE = 
        "create table " + TABLE_DBVERSION + " ("
                + "version integer not null);";
    
    private static final String NOTES_CREATE =
        "create table " + TABLE_NOTES + " ("
            + "id integer primary key autoincrement, "
            + "note text, "
            + "lastedit text);";
    
    private static final String NOTES_DROP =
        "drop table " + TABLE_NOTES + ";";
    
    private SQLiteDatabase db;
    /**
     * 
     * @param ctx
     */
    public DBHelper(Context ctx) {
        myCtx = ctx;
                try {
                        db = myCtx.openOrCreateDatabase(DATABASE_NAME, 0,null);
    
                        // Check for the existence of the DBVERSION table
                        // If it doesn't exist than create the overall data,
                        // otherwise double check the version
                        Cursor c =
                                db.query("sqlite_master", new String[] { "name" },
                                                "type='table' and name='"+TABLE_DBVERSION+"'", null, null, null, null);
                        int numRows = c.getCount();
                        if (numRows < 1) {
                                CreateDatabase(db);
                        } else {
                                int version=0;
                                Cursor vc = db.query(true, TABLE_DBVERSION, new String[] {"version"},
                                                null, null, null, null, null,null);
                                if(vc.getCount() > 0) {
                                    vc.moveToFirst();
                                    version=vc.getInt(0);
                                }
                                vc.close();
                                if (version!=DATABASE_VERSION) {
                                        Log.e(TAG,"database version mismatch");
                                }
                        }
                        c.close();
    
    
                } catch (SQLException e) {
                        Log.d(TAG,"SQLite exception: " + e.getLocalizedMessage());
                } finally {
                        db.close();
                }
    }
    
    private void CreateDatabase(SQLiteDatabase db)
    {
                try {
                        db.execSQL(DBVERSION_CREATE);
                        ContentValues args = new ContentValues();
                        args.put("version", DATABASE_VERSION);
                        db.insert(TABLE_DBVERSION, null, args);
    
                        db.execSQL(NOTES_CREATE);
                        // Populate with data
                        populateDataBaseFromFile();// There are probably better ways to do this.
                        setSharedPreferenceYouPopulatedDB();
                } catch (SQLException e) {
                        Log.d(TAG,"SQLite exception: " + e.getLocalizedMessage());
                } 
    }
    

    Personally I wouldn’t bother with the splash screen, unless you really needed to.

    Another thought might be to:

    1. Write in the db helper a method to determin if your tables exist. Return false if not.
    2. In startup activity call ContentProvider with a request that calls the DBHelper test method.
    3. If false then display splash screen and then call Content Provider to populate DB.
    4. If true, then carry on as normal.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have content management system application that uses a polymorphic tree table as the
I have an application on one domain that displays content from another application from
I have a Silverlight application that reads its content from an XML file. The
I have a web application that has a page that loads the content from
I need to create a pdf file from the HTML provided from database which
I have a multi module maven web application, which uses hibernate. I use the
I have an AppEngine application that uses the blobstore to store user-provided image data.
I have an asp.net application which uses the web part framework to allow users
I've got a hibernate-based application which uses DBUnit for unit testing. We have an
I have an application that uses a mobile ad provider; the way ad provider

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.