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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:29:14+00:00 2026-06-10T08:29:14+00:00

What to display a ProgressDialog when my database is created. This is only showed

  • 0

What to display a ProgressDialog when my database is created. This is only showed once.

I have this code in my activity:

    baseDados.open();
    ListView listContent = (ListView)findViewById(android.R.id.list);
    Cursor query = baseDados.getData(1,0,null);
    MyAdapt cursorAdapter = new MyAdapt(this, query,0);
    listContent.setAdapter(cursorAdapter);

On the first run, database is created and populated and the result of the query is shown on a listview. The code above is used can be used again but this time, the database is not created because it is already created.

On the internet, I saw that the best way to achieve is to use AsyncTaks but I’m having trouble in showing the progressdialog and errors on creating the database if I use AsyncTaks.

I’ve created a class to handle my database and there created my AsyncTaks.

Here is what I’ve done:

public class Database {

    private DbHelper DBHelper;
    private final Context Context;
    private SQLiteDatabase BaseDados;
    static Context ctx;

    private static class DbHelper extends SQLiteOpenHelper {

        public DbHelper(Context context) {
            super(context, BD_NAME, null, BD_VERSION);
            ctx = context;
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            new loadDB().execute(db);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVesion) {
            // Drop table if table exists
            (....)
            // creates new tables and data
            onCreate(db);
        }

        public class loadDB extends AsyncTask<SQLiteDatabase, Void, Integer> {
            ProgressDialog progresso;

            @Override
            protected void onPreExecute() {
                // TODO Auto-generated method stub
                super.onPreExecute();
                progresso = ProgressDialog.show(ctx, "INFO", "Instaling for the first time") );

            }
            @Override
            protected Integer doInBackground(SQLiteDatabase... params) {

                SQLiteDatabase db = params[0];
            // Code to create the tables and populate them
            // Lots of code like below
                db.execSQL("SQL statement goes here");      
                return 1;
            }

            @Override
            protected void onPostExecute(Integer result) {
            // Dismiss the ProgressDialog
                progresso.dismiss();
                super.onPostExecute(result);
            }
        }

    }

    public Database(Context c) {
        Context = c;
    }

    public Database open() throws SQLException {
        DBHelper = new DbHelper(Context);
        BaseDados = DBHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        DBHelper.close();
    }

    public Cursor getData(int tipo, long groupId, String query[]) {
    // querys performed on the database
}
    }

I have two problems.

Problem 1

ProgressDialog is never shown although it takes almost five seconds to create and populate my database

Problem 2

At some point, the app crashes and the database is not correctly created because the querys give error no such table
The code for creating an populating the database is OK because if in the method

@Override
public void onCreate(SQLiteDatabase db) {
    new loadDB().execute(db);
}

I remove new loadDB().execute(db); and put this:

@Override
public void onCreate(SQLiteDatabase db) {
    // SQL code that was on `doInBackground(SQLiteDatabase... params)`
}

Can you tell me what am I doing wrong?

  • 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-10T08:29:15+00:00Added an answer on June 10, 2026 at 8:29 am

    You cannot use an AsyncTask to initialize a db inside constructor, especially because immediately after you call constructor you are calling

      BaseDados = DBHelper.getWritableDatabase();
    

    The AsyncTask would not have been run, so your db would not have been created, but you are trying to get a writable database. A better design is to make everything related to databases synchronous, and then use all db related methods in one Asynctask.

    Also i think it is bad to mix UI and db operations like you have done. You will have to redesign the whole thing

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

Sidebar

Related Questions

I have a progress bar which I create in code my activity. This is
The following code uploads file to the server display progressdialog with percentage without any
I have a main activity that launches a child activity using the following code:
Possible Duplicate: How to display progress dialog before starting an activity in Android? this
I have an Activity which downloads the data from the Database. While the Activity
I try to display a Toast inside a AsyncTask. This first piece of code
I have created a ProgressDialog in android and it works when I do a
hey i want to display a loading progress using ProgressDialog on my aplication, but
Display whitespace characters in Eclipse is a simple trick , but unfortunately this applies
Error display as Incorrect syntax near '@Cmp_DocPath', if i use comment Line code I

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.