0

ファイル CSV から動的テーブルを使用して単純なアクティビティを作成します。ファイルが読み取られて 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 で使用される Type を変更できます。真ん中のものは、publishProgress によって使用されるものです。

于 2012-06-16T15:08:54.823 に答える