0

我使用文件 CSV 的动态表创建简单的 Activity。我想在读取文件和构建 UI 之前简单地显示进度条。我坚持 AsyncTask ..

如果我执行 mytask.get() 则 UI 构建被阻止并且我想要的 progressDialog 将不会显示。但是,如果我调用 mytask.get() 则应用程序崩溃,因为我所有的 UI 都依赖于该 CSV。我找到了这个:

调用 AsyncTask.get() 时未显示 ProgressDialog

与相关:

Android中AsyncTask的通用类?

但我什至无法想象如何在我的案例中使用这个概念..

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);
    }

这是我的任务:

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");
    }
}

这是 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();
}
4

2 回答 2

0

我不确定你为什么使用 get as get

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

你可以简单地使用 onPostExecute 异步地让你的 UI 工作。

并避免在 doInBackground 中与 UI 相关的 wokr 和从 doInBackground 调用的函数...

于 2012-06-16T15:08:22.857 回答
0

很可能您的 add_table 您从 doInBackground 调用的某些代码正在执行某些 UI 功能。由于 doInBackground 不会发生在 UI 线程上,因此您无法在该函数中触摸 UI。相反,您可以使用需要显示的任何数据调用 publishProgress。您可以通过泛型类型声明更改 publishProgress 使用的类型。中间一个是 publishProgress 使用的。

于 2012-06-16T15:08:54.823 回答