0

I am in the latter stages of developing an app that requires a large database import from text files on the first run.

My question is, what is the best way to use a Progress Dialog to show whilst the large import takes place. The section of code that is responsible is shown below (the constructor for this class).

public ManipulateDatabase(Activity contextIn) throws IOException {
        baseHelper = new DatabaseHelper(contextIn);
        context = contextIn;
        BufferedReader pieceScan = new BufferedReader(new InputStreamReader(
                getPieceTextFile(context), "UTF-8"));
        BufferedReader specificScan = new BufferedReader(new InputStreamReader(
                getSpecificTextFile(context), "UTF-8"));
        BufferedReader elementScan = new BufferedReader(new InputStreamReader(
                getElementTextFile(context), "UTF-8"));
        BufferedReader quoteScan = new BufferedReader(new InputStreamReader(
                getQuoteTextFile(context), "UTF-8"));
        openSesame();
        String line;
        String pieceName;
        String isDrama;
        String point;
        String element;
        String quote;
        String isCharacter;
        int commaPoint;
        int commaPoint2;

        if (initialLaunch) {

            System.out.println("INITIAL LAUNCH BLOCK INITIATED");
            while ((line = pieceScan.readLine()) != null) {
                System.out.println("PieceInsertion: ");
                commaPoint = line.indexOf(",");
                pieceName = line.substring(0, commaPoint);
                isDrama = line.substring(commaPoint + 1, commaPoint + 2);
                ContentValues pv = new ContentValues();
                pv.put(pieceField, pieceName);
                pv.put(typeField, (Integer.parseInt(isDrama)));
                dataBase.insert(pieceTable, null, pv);
                System.out.println(line
                        + ": INSERTION SHOULD HAVE JUST FINISHED");
            }

            while ((line = specificScan.readLine()) != null) {
                System.out.println("SpecificInsertion: ");
                commaPoint = line.indexOf(",");
                point = line.substring(0, commaPoint);
                commaPoint2 = (line
                        .substring(commaPoint + 1, line.length() - 1)
                        .indexOf(","))
                        + point.length();
                // pieceName = line.substring(commaPoint + 1, commaPoint2 + 1);
                element = line.substring(commaPoint2 + 2, line.length());

                ContentValues sv = new ContentValues();
                sv.put(pointField, point);
                // sv.put(pieceField, pieceName);
                sv.put(elementField, element);
                dataBase.insert(specificTable, null, sv);
                System.out.println(line
                        + ": INSERTION SHOULD HAVE JUST FINISHED");
            }

            while ((line = elementScan.readLine()) != null) {
                commaPoint = line.indexOf(",");
                element = line.substring(0, commaPoint);
                commaPoint2 = (line
                        .substring(commaPoint + 1, line.length() - 1)
                        .indexOf(","))
                        + element.length();

                pieceName = line.substring(commaPoint + 1, commaPoint2 + 1);
                isCharacter = line.substring(commaPoint2 + 2, commaPoint2 + 3);

                ContentValues ev = new ContentValues();
                ev.put(elementField, element);
                ev.put(pieceField, pieceName);
                ev.put(elTypeField, (Integer.parseInt(isCharacter)));
                dataBase.insert(elementTable, null, ev);
                System.out.println(line
                        + ": INSERTION SHOULD HAVE JUST FINISHED");

            }

            while ((line = quoteScan.readLine()) != null) {
                commaPoint = line.indexOf("^");
                quote = line.substring(0, commaPoint);
                System.out.println("Quote = " + quote);
                point = line.substring(commaPoint + 1, line.length());
                System.out.println("Point = " + point);

                ContentValues qv = new ContentValues();
                qv.put(quoteField, quote);
                qv.put(pointField, point);
                dataBase.insert(quoteTable, null, qv);
                System.out.println(line
                        + ": INSERTION SHOULD HAVE JUST FINISHED");
            }

        }

    }
otherdan
  • 71
  • 1
  • 8

1 Answers1

0

You should use a async task to perform the database loading.

http://developer.android.com/reference/android/os/AsyncTask.html

In the onPreExecute() you can show a Progress Dialog and in the onPostExecute() you should remove the ProgressDialog. The database loading should be done in the doInBackground().

There are many examples for this. Check here for more information

How to use AsyncTask correctly in Android

Your entire ManipulateDatabase() should go in the doInBackground().

Community
  • 1
  • 1
Ajit Pratap Singh
  • 1,299
  • 12
  • 24