0

I create simple Activity with dynamic table from file CSV. I'd like to simply show progress bar before file is read and UI build. I stuck on AsyncTask..

If i execute mytask.get() then the UI building is blocked and my desired progressDialog won't shown. But if i call mytask.get() then app crashes bacause all my UI depends on that CSV. I found this:

ProgressDialog not shown when AsyncTask.get() called

with related:

Common class for AsyncTask in Android?

but i can't even imagine how to use that concept in my case..

public void onCreate(Bundle savedInstanceState) {
            ...
        Log.i(TAG, "before csv:");
        ReadFromCSV mytask = new ReadFromCSV(this);
        mytask.execute(char_name);
//      try {
//          mytask.get();
//      } catch (InterruptedException e1) {
//          // TODO Auto-generated catch block
//          Log.i(TAG, "1 exception");
//          e1.printStackTrace();
//      } catch (ExecutionException e1) {
//          Log.i(TAG, "2 exception");
//          e1.printStackTrace();
//      }
        Log.i(TAG, "after csv");


        // create table from var headers
        set_title_with_charname(flipper.get_moves_group(0)); //set activity title
        Log.i(TAG, "headers values:" + headers);
        heading.create_header(this, headers, 0);
        lay.addView(heading);
        lay.addView(flipper);
        lay.setOrientation(1);

        setContentView(lay);
    }

here is mytask:

private class ReadFromCSV extends AsyncTask<String, Void, String> {

    public Activity activity;
    private ProgressDialog Dialog = new ProgressDialog(MovesList.this);

    public ReadFromCSV(Activity a) {
        activity = a;
    }

    protected void onPreExecute() {
        Log.i(TAG, "start onPreExecute");
        Dialog.setMessage("Please wait...");
        Dialog.show();

        Log.i(TAG, "END onPreExecute");
    }

    protected String doInBackground(String... urls) {
        Log.i(TAG, "doInBackground");
        // return loadImageFromNetwork(urls[0]);
        String new_val = urls[0] + "doInBack";
        Log.i(TAG, "new_val: " + new_val);


        try {
            readfromcsv(flipper, char_name);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Log.i(TAG, "END doInBackground");

        return new_val;
    }

    protected void onPostExecute(String result) {
        Log.i(TAG, "onpostexecute");
        Log.i(TAG, "result: " + result);

        try {
            if(Dialog.isShowing()) {
                Dialog.dismiss();
            }
                    // do your Display and data setting operation here
        }
        catch(Exception e) {
            Log.i(TAG, "exception: ");
        }

        Log.i(TAG, "END onPostExecute");
    }
}

here is a code of readfromcsv:

public void readfromcsv(MyFlipper flipper, String char_name)
        throws IOException {

    String filename = char_name + ".csv";
    InputStream is = getAssets().open(filename);
    BufferedReader in = new BufferedReader(new InputStreamReader(is,
            "UTF-8"));

    String reader = "";
    int line_nb = 0;
    MyTable table = null;
    while ((reader = in.readLine()) != null) {
        line_nb += 1;
        Log.d(TAG, "file line: " + reader);
        String[] row_data = reader.split("ą");
        Log.d(TAG, "splitted: " + row_data[0] + ',' + row_data[1] + "..");
        if (line_nb == 1) {
            Log.i(TAG, "first row - memorized headers..");
            headers = row_data;
            Log.i(TAG, "headers memorized: " + row_data[0] + ","
                    + row_data[1] + "..");
            continue;
        }

        if (row_data[0].equals("")) {
            // new table
            // Log.i(TAG, "new moves_group..");
            if (table != null) {

                add_table(table);
            }
            Log.d(TAG, "creating new table");
            table = new MyTable(this, true, row_data[1]);
            Log.d(TAG, "new table created");
            continue;
        }
        Log.d(TAG, "regular row..");
        table.row_from_template(this, row_data, line_nb % 2);
        if (line_nb == 60) {
            break;
        }
        ;
    }
    add_table(table);
    in.close();
}
Community
  • 1
  • 1
xliiv
  • 5,399
  • 5
  • 29
  • 35

2 Answers2

0

I am not sure in you case why you are using get as get

Waits if necessary for the computation to complete, and then retrieves its result.

you can simply do you UI work Asynchronously using onPostExecute.

And Avoid UI related wokr in doInBackground and function called from doInBackground ...

Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
0

Most likely either your add_table your some code that you are calling from doInBackground is executing some UI functions. Since doInBackground does not happen on the UI thread you can't touch the UI in that function. Instead what you could do is call publishProgress with whatever data you need to display. You can alter the Type that is used by publishProgress through the generic type declaration. The middle one is what is used by publishProgress.

Matt Wolfe
  • 8,924
  • 8
  • 60
  • 77