Skip to main content
deleted 30 characters in body
Source Link
Willi Mentzel
  • 30.2k
  • 21
  • 120
  • 130

I hope that helps!

Mark

I hope that helps!

Mark

Added missed line of code `publishProgress(counter);`
Source Link
MarkJ
  • 106
  • 8

EDIT: publishProgress(counter); passes the value of where the task has got to to onProgressUpdate().

    private class LoadViewTask extends AsyncTask<Void, Integer, Void> {
    
    private Context context;
    public LoadViewTask(Context context) {
        this.context = context.getApplicationContext();
    }

    @Override
    protected void onPreExecute() {

        progressDialog = new ProgressDialog(MainActivity.this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setTitle("Loading...");
        progressDialog.setMessage("Initializing data for first use!");
        progressDialog.setCancelable(false);
        progressDialog.setIndeterminate(false);

        // this counts how many line to be added to the database so it can later tell how far it has got.
        final Resources resources2 = context.getResources();
        InputStream inputStream2 = resources2.openRawResource(R.raw.rawherbaldata);
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream2));
        int lineCount = 0;

        try {
            String line;
            while ((line = reader.readLine()) != null) {
                lineCount++;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        progressDialog.setMax(lineCount);
        progressDialog.setProgress(0);
        progressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        HerbalDatabaseOpenHelper mHerbalDbHelper = new HerbalDatabaseOpenHelper(MainActivity.this);
        SQLiteDatabase db = mHerbalDbHelper.getWritableDatabase();
        
        int counter = 0;

        final Resources resources2 = context.getResources();
        InputStream inputStream2 = resources2.openRawResource(R.raw.rawherbaldata);
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream2));

        db.beginTransaction();
        try {
            int lineNumber = 1;
            String line;
            while ((line = reader.readLine()) != null) {
                
            // CODE FOR ENTERING LINE INTO DATABASE         

            // EDIT: the following keeps the task updated on where it has got to, passing the count to onProgressUpdate()
            counter++;
            publishProgress(counter);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        db.setTransactionSuccessful();
        db.endTransaction();
        db.close();

        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        progressDialog.setProgress(values[0]);
    }

    @Override
    protected void onPostExecute(Void result) {
        progressDialog.dismiss();

        SharedPreferences sharedPref = getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
        pxsRxsUpdate = true;
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putBoolean(PXS_RXS_UPDATE, pxsRxsUpdate);
        editor.commit();

        // initialize the View
        setContentView(R.layout.activity_main);
    }
}
    private class LoadViewTask extends AsyncTask<Void, Integer, Void> {
    
    private Context context;
    public LoadViewTask(Context context) {
        this.context = context.getApplicationContext();
    }

    @Override
    protected void onPreExecute() {

        progressDialog = new ProgressDialog(MainActivity.this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setTitle("Loading...");
        progressDialog.setMessage("Initializing data for first use!");
        progressDialog.setCancelable(false);
        progressDialog.setIndeterminate(false);

        // this counts how many line to be added to the database so it can later tell how far it has got.
        final Resources resources2 = context.getResources();
        InputStream inputStream2 = resources2.openRawResource(R.raw.rawherbaldata);
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream2));
        int lineCount = 0;

        try {
            String line;
            while ((line = reader.readLine()) != null) {
                lineCount++;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        progressDialog.setMax(lineCount);
        progressDialog.setProgress(0);
        progressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        HerbalDatabaseOpenHelper mHerbalDbHelper = new HerbalDatabaseOpenHelper(MainActivity.this);
        SQLiteDatabase db = mHerbalDbHelper.getWritableDatabase();
        
        int counter = 0;

        final Resources resources2 = context.getResources();
        InputStream inputStream2 = resources2.openRawResource(R.raw.rawherbaldata);
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream2));

        db.beginTransaction();
        try {
            int lineNumber = 1;
            String line;
            while ((line = reader.readLine()) != null) {
                
            // CODE FOR ENTERING LINE INTO DATABASE

            counter++;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        db.setTransactionSuccessful();
        db.endTransaction();
        db.close();

        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        progressDialog.setProgress(values[0]);
    }

    @Override
    protected void onPostExecute(Void result) {
        progressDialog.dismiss();

        SharedPreferences sharedPref = getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
        pxsRxsUpdate = true;
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putBoolean(PXS_RXS_UPDATE, pxsRxsUpdate);
        editor.commit();

        // initialize the View
        setContentView(R.layout.activity_main);
    }
}

EDIT: publishProgress(counter); passes the value of where the task has got to to onProgressUpdate().

    private class LoadViewTask extends AsyncTask<Void, Integer, Void> {
    
    private Context context;
    public LoadViewTask(Context context) {
        this.context = context.getApplicationContext();
    }

    @Override
    protected void onPreExecute() {

        progressDialog = new ProgressDialog(MainActivity.this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setTitle("Loading...");
        progressDialog.setMessage("Initializing data for first use!");
        progressDialog.setCancelable(false);
        progressDialog.setIndeterminate(false);

        // this counts how many line to be added to the database so it can later tell how far it has got.
        final Resources resources2 = context.getResources();
        InputStream inputStream2 = resources2.openRawResource(R.raw.rawherbaldata);
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream2));
        int lineCount = 0;

        try {
            String line;
            while ((line = reader.readLine()) != null) {
                lineCount++;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        progressDialog.setMax(lineCount);
        progressDialog.setProgress(0);
        progressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        HerbalDatabaseOpenHelper mHerbalDbHelper = new HerbalDatabaseOpenHelper(MainActivity.this);
        SQLiteDatabase db = mHerbalDbHelper.getWritableDatabase();
        
        int counter = 0;

        final Resources resources2 = context.getResources();
        InputStream inputStream2 = resources2.openRawResource(R.raw.rawherbaldata);
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream2));

        db.beginTransaction();
        try {
            int lineNumber = 1;
            String line;
            while ((line = reader.readLine()) != null) {
                
            // CODE FOR ENTERING LINE INTO DATABASE         

            // EDIT: the following keeps the task updated on where it has got to, passing the count to onProgressUpdate()
            counter++;
            publishProgress(counter);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        db.setTransactionSuccessful();
        db.endTransaction();
        db.close();

        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        progressDialog.setProgress(values[0]);
    }

    @Override
    protected void onPostExecute(Void result) {
        progressDialog.dismiss();

        SharedPreferences sharedPref = getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
        pxsRxsUpdate = true;
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putBoolean(PXS_RXS_UPDATE, pxsRxsUpdate);
        editor.commit();

        // initialize the View
        setContentView(R.layout.activity_main);
    }
}
Added clarification
Source Link
MarkJ
  • 106
  • 8

I should say that although it does it asynchronously because the main layout hasn't loaded the user has to wait for the loading to complete before he or she can continue, so hopefully that means it doing it asynchronously won't be a problem for you with the app depending on the database.

I should say that although it does it asynchronously because the main layout hasn't loaded the user has to wait for the loading to complete before he or she can continue, so hopefully that means it doing it asynchronously won't be a problem for you with the app depending on the database.

Source Link
MarkJ
  • 106
  • 8
Loading