38

長い計算を行うアプリケーションがあり、実行中に進行状況ダイアログを表示したいと考えています。これまでのところ、スレッド/ハンドラーでこれを実行できることがわかりましたが、機能しませんでしたAsyncTask

私のアプリケーションでは、マーカー付きのマップを使用し、定義したメソッドを呼び出す onTap 関数を実装しました。AsyncTaskこのメソッドは、はい/いいえボタンを含むダイアログを作成し、はいがクリックされた場合に呼び出したいと思います。私の質問は、 を に渡す方法ArrayList<String>(AsyncTaskおよびそこで操作する方法) と、 ?ArrayList<String>から新しいような結果を取得する方法です。AsyncTask

メソッドのコードは次のようになります。

String curloc = current.toString();
String itemdesc = item.mDescription;

ArrayList<String> passing = new ArrayList<String>();
passing.add(itemdesc);
passing.add(curloc);

ArrayList<String> result = new ArrayList<String>();

new calc_stanica().execute(passing,result);

String minim = result.get(0);
int min = Integer.parseInt(minim);

String glons = result.get(1);
String glats = result.get(2);

double glon = Double.parseDouble(glons);
double glat = Double.parseDouble(glats);

GeoPoint g = new GeoPoint(glon, glat);
String korisni_linii = result.get(3);

AsyncTaskご覧のとおり、「渡す」文字列配列リストを に送信し、そこから「結果」文字列配列リストを取得したいと思います。calc_stanicaAssycTaskクラスは次のようになります。

public class calc_stanica extends AsyncTask<ArrayList<String>, Void, ArrayList<String>> {
    ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        dialog = new ProgressDialog(baraj_mapa.this);
        dialog.setTitle("Calculating...");
        dialog.setMessage("Please wait...");
        dialog.setIndeterminate(true);
        dialog.show();
    }

    protected ArrayList<String> doInBackground(ArrayList<String>... passing) {

        //Some calculations...

        return something; //???
    }

    protected void onPostExecute(Void unused) {
        dialog.dismiss();
    }

したがって、私の質問は、メソッドで「渡す」配列リストの要素を取得するAsyncTask doInBackground方法 (およびそこで使用する方法) と、メイン メソッドで使用する配列リストを返す方法 (「結果」配列リスト) です。

4

5 に答える 5

66

メソッドを次のように変更します。

String curloc = current.toString();
String itemdesc = item.mDescription;
ArrayList<String> passing = new ArrayList<String>();
passing.add(itemdesc);
passing.add(curloc);
new calc_stanica().execute(passing); //no need to pass in result list

そして、非同期タスクの実装を変更します

public class calc_stanica extends AsyncTask<ArrayList<String>, Void, ArrayList<String>> {
ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        dialog = new ProgressDialog(baraj_mapa.this);
        dialog.setTitle("Calculating...");
        dialog.setMessage("Please wait...");
        dialog.setIndeterminate(true);
        dialog.show();
    }

    protected ArrayList<String> doInBackground(ArrayList<String>... passing) {
        ArrayList<String> result = new ArrayList<String>();
        ArrayList<String> passed = passing[0]; //get passed arraylist

        //Some calculations...

        return result; //return result
    }

    protected void onPostExecute(ArrayList<String> result) {
        dialog.dismiss();
        String minim = result.get(0);
        int min = Integer.parseInt(minim);
        String glons = result.get(1);
        String glats = result.get(2);
        double glon = Double.parseDouble(glons);
        double glat = Double.parseDouble(glats);
        GeoPoint g = new GeoPoint(glon, glat);
        String korisni_linii = result.get(3);
    }

更新:

タスク開始コンテキストにアクセスしたい場合、最も簡単な方法は onPostExecute をオーバーライドすることです。

new calc_stanica() {
    protected void onPostExecute(ArrayList<String> result) {
      // here you have access to the context in which execute was called in first place. 
      // You'll have to mark all the local variables final though..
     }
}.execute(passing);
于 2010-11-16T15:57:24.517 に答える
12

なぜArrayListを渡すのですか?? パラメータを直接指定して execute を呼び出すだけでよいはずです。

String curloc = current.toString();
String itemdesc = item.mDescription;
new calc_stanica().execute(itemdesc, curloc)

それが可変引数の仕組みですよね?ArrayList を作成して変数を渡すのは二重の作業です。

于 2012-05-21T09:30:26.087 に答える
4

私はこれに傾倒することに同意します。

電話:

new calc_stanica().execute(stringList.toArray(new String[stringList.size()]));

タスク:

public class calc_stanica extends AsyncTask<String, Void, ArrayList<String>> {
        @Override
        protected ArrayList<String> doInBackground(String... args) {
           ...
        }

        @Override
        protected void onPostExecute(ArrayList<String> result) {
           ... //do something with the result list here
        }
}

または、結果リストをクラスパラメータにして、ArrayListをブール値(成功/失敗)に置き換えることもできます。

public class calc_stanica extends AsyncTask<String, Void, Boolean> {
        private List<String> resultList;

        @Override
        protected boolean doInBackground(String... args) {
           ...
        }

        @Override
        protected void onPostExecute(boolean success) {
           ... //if successfull, do something with the result list here
        }
}
于 2012-10-30T11:45:12.203 に答える
1

私はこのようにしないでください。asychtask クラスのコンストラクターをオーバーロードする方が簡単だと思います..

public class calc_stanica extends AsyncTask>

String String mWhateveryouwantToPass;

 public calc_stanica( String whateveryouwantToPass)
{

    this.String mWhateveryouwantToPass = String whateveryouwantToPass;
}
/*Now you can use  whateveryouwantToPass in the entire asynchTask ... you could pass in a context to your activity and try that too.*/   ...  ...  
于 2013-01-24T02:56:24.873 に答える
0

次のような戻り結果を受け取ることができます AsyncTask

@Override
protected Boolean doInBackground(Void... params) {
    if (host.isEmpty() || dbName.isEmpty() || user.isEmpty() || pass.isEmpty() || port.isEmpty()) {
        try {
            throw new SQLException("Database credentials missing");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    try {
        Class.forName("org.postgresql.Driver");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    try {
        this.conn = DriverManager.getConnection(this.host + ':' + this.port + '/' + this.dbName, this.user, this.pass);
    } catch (SQLException e) {
        e.printStackTrace();
    }

    return true;
}

受信クラス:

_store.execute();
boolean result =_store.get();

それが役立つことを願っています。

于 2015-04-22T21:20:26.473 に答える