Display progress while doInbackground

烂漫一生 提交于 2020-01-06 12:51:28

问题


While this code works, but the blank screen appears until doInBackground is complete

package com.vaishnavismeclass.english.naalaayiradivyaprabandam;



import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.AsyncTask;
import android.util.Log;

public class Divyadesamdb extends AsyncTask<String, Void, String>{

    public static final String KEY_ROWID = "_id";
    public static final String KEY_CODE = "code";
    public static final String KEY_NAME = "name";
    public static final String KEY_CONTINENT = "continent";

    private static final String TAG = "CountriesDbAdapter";
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;

    private final Context mCtx;

    /*
     * My code starts here
     */
    // Table prabandham
    public static final String TABLE_PRABANDHAMS = "prabandhams";
    public static final String PRABANDHAMS_COLUMN_ID = "_id";
    public static final String PRABANDHAMS_COLUMN_PRABANDHAM = "prabandham";
    public static final String PRABANDHAMS_COLUMN_FROM = "from";
    public static final String PRABANDHAMS_COLUMN_ENDING = "ending";
    public static final String PRABANDHAMS_COLUMN_TOTAL = "total";
    public static final String PRABANDHAMS_COLUMN_AZHWAAR = "azhwaar";
    public static final String PRABANDHAMS_COLUMN_CLASSIFICATION = "classification";

    // Table prabandham
    public static final String TABLE_DIVYADESAMS = "divyadesams";

    public static final String DIVYADESAMS_COLUMN_ID = "_id";
    public static final String DIVYADESAMS_COLUMN_DDD = "ddd";
    public static final String DIVYADESAMS_COLUMN_MANDALAM = "mandalam";
    public static final String DIVYADESAMS_COLUMN_TRAVELBASE = "travelbase";
    public static final String DIVYADESAMS_COLUMN_DIVYADESAM = "divyadesam";
    public static final String DIVYADESAMS_COLUMN_LOCATION = "location";
    public static final String DIVYADESAMS_COLUMN_DISTANCE = "distance";
    public static final String DIVYADESAMS_COLUMN_ROUTE = "route";
    public static final String DIVYADESAMS_COLUMN_POIGAIAZHWAAR = "poigaiazhwaar";
    public static final String DIVYADESAMS_COLUMN_BHOODHATHAZHWAAR = "bhoodhathazhwaar";
    public static final String DIVYADESAMS_COLUMN_PEYAAZHWAAR = "peyaazhwaar";
    public static final String DIVYADESAMS_COLUMN_TIRUMAZHISAIAZHWAAR = "tirumazhisaiazhwaar";
    public static final String DIVYADESAMS_COLUMN_NAMMAZHWAAR = "nammazhwaar";
    public static final String DIVYADESAMS_COLUMN_KULASEKARAAZHWAAR = "kulasekaraazhwaar";
    public static final String DIVYADESAMS_COLUMN_PERIAAHZWAR = "periaahzwar";
    public static final String DIVYADESAMS_COLUMN_ANDAL = "andal";
    public static final String DIVYADESAMS_COLUMN_THONDARADIPPODIAZHWAAR = "thondaradippodiazhwaar";
    public static final String DIVYADESAMS_COLUMN_TIRUMANGAIAZHWAAR = "tirumangaiazhwaar";
    public static final String DIVYADESAMS_COLUMN_TIRUPPANAZHWAAR = "tiruppanazhwaar";
    public static final String DIVYADESAMS_COLUMN_TOTALPAASURAMS = "totalpaasurams";
    public static final String DIVYADESAMS_COLUMN_NOOFAZHWAARSSUNG = "noofazhwaarssung";
    public static final String DIVYADESAMS_COLUMN_GEOCODE = "geocode";
    public static final String DIVYADESAMS_COLUMN_FACINGPOSITION = "facingposition";
    public static final String DIVYADESAMS_COLUMN_TIRUKKOLAM = "tirukkolam";
    public static final String DIVYADESAMS_COLUMN_THAYAR = "thayar";
    public static final String DIVYADESAMS_COLUMN_MOOLAVAR = "moolavar";
    public static final String DIVYADESAMS_COLUMN_UTSAVAR = "utsavar";
    public static final String DIVYADESAMS_COLUMN_VIMAANAM = "vimaanam";
    public static final String DIVYADESAMS_COLUMN_PUSHKARANI = "pushkarani";
    public static final String DIVYADESAMS_COLUMN_AAGAMAM = "aagamam";
    public static final String DIVYADESAMS_COLUMN_SAMPRADHAYAM = "sampradhayam";
    public static final String DIVYADESAMS_COLUMN_OTHERNAME = "othername";

    // table azhwaar
    public static final String TABLE_AZHWAARS = "azhwaars";

    public static final String AZHWAARS_COLUMN_ID = "_id";
    public static final String AZHWAARS_COLUMN_NAME = "name";
    public static final String AZHWAARS_COLUMN_ORIGIN = "origin";
    public static final String AZHWAARS_COLUMN_GEOCODE = "geocode";
    public static final String AZHWAARS_COLUMN_CALENDAR = "calendar";
    public static final String AZHWAARS_COLUMN_NAKSHATRAM = "nakshatram";
    public static final String AZHWAARS_COLUMN_PRABANDAM = "prabandam";
    public static final String AZHWAARS_COLUMN_PERIOD = "period";
    public static final String AZHWAARS_COLUMN_NAMMAZHWAAR_AMSAM = "nammazhwaar_amsam";
    public static final String AZHWAARS_COLUMN_NITYASURI_AMSAM = "nityasuri_amsam";
    public static final String AZHWAARS_COLUMN_OTHERNAMES = "othernames";
    public static final String AZHWAARS_COLUMN_MANGALASAASANAM = "mangalasaasanam";

    // table pasurams
    public static final String TABLE_PASURAMS = "pasurams";

    public static final String PASURAMS_COLUMN_ID = "_id";
    public static final String PASURAMS_COLUMN_PAASURAMNUMBER = "paasuramnumber";
    public static final String PASURAMS_COLUMN_AZHWAAR = "azhwaar";
    public static final String PASURAMS_COLUMN_AAYIRAM = "aayiram";
    public static final String PASURAMS_COLUMN_CATEGORY = "category";
    public static final String PASURAMS_COLUMN_SUBCATEGORY = "subcategory";
    public static final String PASURAMS_COLUMN_TITLE = "title";
    public static final String PASURAMS_COLUMN_TITLENUMBER = "titlenumber";
    public static final String PASURAMS_COLUMN_MANGALASASANAMON = "mangalasasanamon";
    public static final String PASURAMS_COLUMN_PAASURAM_EN_STR = "paasuram_en_str";
    public static final String PASURAMS_COLUMN_IMAGE_ID = "imageid";

    private static final String DATABASE_NAME = "divyaprabandam.db";
    private static final int DATABASE_VERSION = 4;

    // Database creation sql statement
    private static final String PASURAM_CREATE = "CREATE TABLE  "
            + TABLE_PASURAMS + " (" + PASURAMS_COLUMN_ID
            + " integer PRIMARY KEY autoincrement,"
            + PASURAMS_COLUMN_PAASURAMNUMBER + " Text,"
            + PASURAMS_COLUMN_AAYIRAM + " Text," + PASURAMS_COLUMN_AZHWAAR
            + " Text," + PASURAMS_COLUMN_CATEGORY + " Text,"
            + PASURAMS_COLUMN_SUBCATEGORY + " Text," + PASURAMS_COLUMN_TITLE
            + " Text," + PASURAMS_COLUMN_TITLENUMBER + " Text,"
            + PASURAMS_COLUMN_MANGALASASANAMON + " Text,"
            + PASURAMS_COLUMN_PAASURAM_EN_STR + " Text,"
            + PASURAMS_COLUMN_IMAGE_ID + " integer);";

    /*
     * My code ends here
     */
    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            Log.w(TAG, PASURAM_CREATE);
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_PASURAMS);
            db.execSQL(PASURAM_CREATE);

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");

            db.execSQL("DROP TABLE IF EXISTS " + TABLE_PASURAMS);

            onCreate(db);
        }
    }

    public Divyadesamdb(Context ctx) {
        this.mCtx = ctx;
    }

    public Divyadesamdb open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        if (mDbHelper != null) {
            mDbHelper.close();
        }
    }

    public long createPasuram(String pasuramNumber, String Azhwaar,
            String Aayiram, String Category, String SubCategory, String Title,
            String TitleNumber, String MangalasasanamOn, String Paasuram_en_str) {

        ContentValues initialValues = new ContentValues();
        initialValues.put("paasuramnumber", pasuramNumber);
        initialValues.put("aayiram", Aayiram);
        initialValues.put("azhwaar", Azhwaar);
        initialValues.put("category", Category);
        initialValues.put("subcategory", SubCategory);
        initialValues.put("title", Title);
        initialValues.put("titlenumber", TitleNumber);
        initialValues.put("mangalasasanamon", MangalasasanamOn);
        initialValues.put("paasuram_en_str", Paasuram_en_str);
        if (Azhwaar.equals("Poigai Azhwaar"))
            initialValues.put("imageid", R.drawable.poigai);
        if (Azhwaar.equals("Bhoodhath Azhwaar"))
            initialValues.put("imageid", R.drawable.bhoodath);
        if (Azhwaar.equals("Peya Azhwaar"))
            initialValues.put("imageid", R.drawable.pey);
        if (Azhwaar.equals("Tirumazhisai Azhwaar"))
            initialValues.put("imageid", R.drawable.thirumazhisai);
        if (Azhwaar.equals("Namm Azhwaar"))
            initialValues.put("imageid", R.drawable.nammalwar);
        if (Azhwaar.equals("Madhurakavi Azhwaar"))
            initialValues.put("imageid", R.drawable.madhurakavi);
        if (Azhwaar.equals("Kulasekara Azhwaar"))
            initialValues.put("imageid", R.drawable.kulasekara);
        if (Azhwaar.equals("PeriyAzhwaar"))
            initialValues.put("imageid", R.drawable.perialwar);
        if (Azhwaar.equals("Andal"))
            initialValues.put("imageid", R.drawable.andal);
        if (Azhwaar.equals("Thondaradippodi Azhwaar"))
            initialValues.put("imageid", R.drawable.thondaradipodi);
        if (Azhwaar.equals("Tiruppaan Azhwaar"))
            initialValues.put("imageid", R.drawable.tiruppan);
        if (Azhwaar.equals("Tirumangai Azhwaar"))
            initialValues.put("imageid", R.drawable.tirumangai);

        return mDb.insert(TABLE_PASURAMS, null, initialValues);

    }

    public boolean deleteAllPasurams() {

        int doneDelete = 0;
        doneDelete = mDb.delete(TABLE_PASURAMS, null, null);
        Log.w(TAG, Integer.toString(doneDelete));
        return doneDelete > 0;

    }

    public int getCountAllPasurams() {

        int totalPasurams = 0;
        String countQuery = "SELECT  * FROM " + TABLE_PASURAMS;
        Cursor cursor = mDb.rawQuery(countQuery, null);
        totalPasurams = cursor.getCount();
        cursor.close();
        Log.w(TAG, Integer.toString(totalPasurams));
        return totalPasurams;

    }

    public Cursor fetchAllPasurams(String pasuramNumber, String aayiram,
            String azhwaar, String category, String mangalasasanamOn,
            String subCategory, String title, String from, String ending) {
        Cursor mCursor = mDb.query(TABLE_PASURAMS, new String[] {
                PASURAMS_COLUMN_ID, PASURAMS_COLUMN_PAASURAMNUMBER,
                PASURAMS_COLUMN_AAYIRAM, PASURAMS_COLUMN_AZHWAAR,
                PASURAMS_COLUMN_CATEGORY, PASURAMS_COLUMN_MANGALASASANAMON,
                PASURAMS_COLUMN_PAASURAM_EN_STR, PASURAMS_COLUMN_SUBCATEGORY,
                PASURAMS_COLUMN_TITLE, PASURAMS_COLUMN_TITLENUMBER,
                PASURAMS_COLUMN_IMAGE_ID }, PASURAMS_COLUMN_CATEGORY
                + " LIKE ? AND " + PASURAMS_COLUMN_SUBCATEGORY + " LIKE ? AND "
                + PASURAMS_COLUMN_TITLE + " LIKE ?", new String[] {
                "%" + category + "%", "%" + subCategory + "%",
                "%" + title + "%" }, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();

        }

        return mCursor;
    }

    public void insertPasurams() {
/*
        ContentValues _pasuramsValues = new ContentValues();
        Resources res = mCtx.getResources();
        String[] _pasuramsTable_Records = res.getStringArray(R.array.pasurams);
        // Loop trough array and insert records into table
        int _Length = _pasuramsTable_Records.length;

        int progressPercent = 0;

        ProgressDialog barProgressDialog = new ProgressDialog(mCtx);
        barProgressDialog.setTitle("Setting up Paasurams ...");
        barProgressDialog.setMessage("Creating Paasurams ...");
        barProgressDialog.setProgressStyle(barProgressDialog.STYLE_HORIZONTAL);
        barProgressDialog.setProgress(0);
        barProgressDialog.setMax(4535);
        barProgressDialog.show();

        for (int i = 0; i < _Length; i++) {
            /*
             * Update the progress on every 45 for 99 times
             * reminder move to 100%
             * 
             */
            /*
            barProgressDialog.incrementProgressBy(1);
            if ( (i % 45) == 0)
            {
            progressPercent++;
            barProgressDialog.incrementProgressBy(1);
            }

            if (progressPercent==99){
                progressPercent++;
                barProgressDialog.incrementProgressBy(1);
            }
            */
            //
            // split the csv string to columns
            //
            //String[] pasuramsSplitString = _pasuramsTable_Records[i].split(",");
            //
            // store the values into collection
            /*
            _pasuramsValues.put("paasuramnumber", pasuramsSplitString[0]);
            _pasuramsValues.put("aayiram", pasuramsSplitString[2]);
            _pasuramsValues.put("azhwaar", pasuramsSplitString[1]);
            _pasuramsValues.put("category", pasuramsSplitString[3]);
            _pasuramsValues.put("subcategory", pasuramsSplitString[4]);
            _pasuramsValues.put("title", pasuramsSplitString[5]);
            _pasuramsValues.put("titlenumber", pasuramsSplitString[6]);
            _pasuramsValues.put("mangalasasanamon", pasuramsSplitString[7]);
            _pasuramsValues.put("paasuram_en_str", pasuramsSplitString[8]);
            if (pasuramsSplitString[1].equals("Poigai Azhwaar"))
                _pasuramsValues.put("imageid", R.drawable.poigai);
            if (pasuramsSplitString[1].equals("Bhoodhath Azhwaar"))
                _pasuramsValues.put("imageid", R.drawable.bhoodath);
            if (pasuramsSplitString[1].equals("Peya Azhwaar"))
                _pasuramsValues.put("imageid", R.drawable.pey);
            if (pasuramsSplitString[1].equals("Tirumazhisai Azhwaar"))
                _pasuramsValues.put("imageid", R.drawable.thirumazhisai);
            if (pasuramsSplitString[1].equals("Namm Azhwaar"))
                _pasuramsValues.put("imageid", R.drawable.nammalwar);
            if (pasuramsSplitString[1].equals("Madhurakavi Azhwaar"))
                _pasuramsValues.put("imageid", R.drawable.madhurakavi);
            if (pasuramsSplitString[1].equals("Kulasekara Azhwaar"))
                _pasuramsValues.put("imageid", R.drawable.kulasekara);
            if (pasuramsSplitString[1].equals("PeriyAzhwaar"))
                _pasuramsValues.put("imageid", R.drawable.perialwar);
            if (pasuramsSplitString[1].equals("Andal"))
                _pasuramsValues.put("imageid", R.drawable.andal);
            if (pasuramsSplitString[1].equals("Thondaradippodi Azhwaar"))
                _pasuramsValues.put("imageid", R.drawable.thondaradipodi);
            if (pasuramsSplitString[1].equals("Tiruppaan Azhwaar"))
                _pasuramsValues.put("imageid", R.drawable.tiruppan);
            if (pasuramsSplitString[1].equals("Tirumangai Azhwaar"))
                _pasuramsValues.put("imageid", R.drawable.tirumangai);

            mDb.insert("pasurams", null, _pasuramsValues);
        }*/
        doInBackground("Hello");
    }

    @Override
    protected String doInBackground(String... arg0) {
        ContentValues _pasuramsValues = new ContentValues();
        Resources res = mCtx.getResources();
        String[] _pasuramsTable_Records = res.getStringArray(R.array.pasurams);
        // Loop trough array and insert records into table
        int _Length = _pasuramsTable_Records.length;
        int progressPercent = 0;

        ProgressDialog barProgressDialog = new ProgressDialog(mCtx);
        barProgressDialog.setTitle("Setting up Paasurams ...");
        barProgressDialog.setMessage("Creating Paasurams ...");
        barProgressDialog.setProgressStyle(barProgressDialog.STYLE_HORIZONTAL);
        barProgressDialog.setProgress(0);
        barProgressDialog.setMax(4535);
        barProgressDialog.show();

        for (int i = 0; i < _Length; i++) {
            /*
             * Update the progress on every 45 for 99 times
             * reminder move to 100%
             * 
             */

            barProgressDialog.incrementProgressBy(1);
            if ( (i % 45) == 0)
            {
            progressPercent++;
            barProgressDialog.incrementProgressBy(1);
            }

            if (progressPercent==99){
                progressPercent++;
                barProgressDialog.incrementProgressBy(1);
            }
            //
            // split the csv string to columns
            //
            String[] pasuramsSplitString = _pasuramsTable_Records[i].split(",");
            //
            // store the values into collection
            //
            _pasuramsValues.put("paasuramnumber", pasuramsSplitString[0]);
            _pasuramsValues.put("aayiram", pasuramsSplitString[2]);
            _pasuramsValues.put("azhwaar", pasuramsSplitString[1]);
            _pasuramsValues.put("category", pasuramsSplitString[3]);
            _pasuramsValues.put("subcategory", pasuramsSplitString[4]);
            _pasuramsValues.put("title", pasuramsSplitString[5]);
            _pasuramsValues.put("titlenumber", pasuramsSplitString[6]);
            _pasuramsValues.put("mangalasasanamon", pasuramsSplitString[7]);
            _pasuramsValues.put("paasuram_en_str", pasuramsSplitString[8]);
            if (pasuramsSplitString[1].equals("Poigai Azhwaar"))
                _pasuramsValues.put("imageid", R.drawable.poigai);
            if (pasuramsSplitString[1].equals("Bhoodhath Azhwaar"))
                _pasuramsValues.put("imageid", R.drawable.bhoodath);
            if (pasuramsSplitString[1].equals("Peya Azhwaar"))
                _pasuramsValues.put("imageid", R.drawable.pey);
            if (pasuramsSplitString[1].equals("Tirumazhisai Azhwaar"))
                _pasuramsValues.put("imageid", R.drawable.thirumazhisai);
            if (pasuramsSplitString[1].equals("Namm Azhwaar"))
                _pasuramsValues.put("imageid", R.drawable.nammalwar);
            if (pasuramsSplitString[1].equals("Madhurakavi Azhwaar"))
                _pasuramsValues.put("imageid", R.drawable.madhurakavi);
            if (pasuramsSplitString[1].equals("Kulasekara Azhwaar"))
                _pasuramsValues.put("imageid", R.drawable.kulasekara);
            if (pasuramsSplitString[1].equals("PeriyAzhwaar"))
                _pasuramsValues.put("imageid", R.drawable.perialwar);
            if (pasuramsSplitString[1].equals("Andal"))
                _pasuramsValues.put("imageid", R.drawable.andal);
            if (pasuramsSplitString[1].equals("Thondaradippodi Azhwaar"))
                _pasuramsValues.put("imageid", R.drawable.thondaradipodi);
            if (pasuramsSplitString[1].equals("Tiruppaan Azhwaar"))
                _pasuramsValues.put("imageid", R.drawable.tiruppan);
            if (pasuramsSplitString[1].equals("Tirumangai Azhwaar"))
                _pasuramsValues.put("imageid", R.drawable.tirumangai);


            mDb.insert("pasurams", null, _pasuramsValues);
        }
        return null;
    }

}

After that the progressdialog is display. Would like progressdialog to work while loop is on. Any help is appreciated


回答1:


you can use AsyncTask..

check this working code...

to call AsyncTask use new BackGround().execute();

private class BackGround extends AsyncTask < Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(Instructions.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // here is your background process...
        return null;

    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        if (pDialog.isShowing())
            pDialog.dismiss();
    }
}


来源:https://stackoverflow.com/questions/22985225/display-progress-while-doinbackground

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!