将参数传递给AsyncTask,并返回结果

时间:2010-11-16 15:14:42

标签: android return android-asynctask progressdialog

我有一个应用程序做了一些长时间的计算,我想在完成后显示进度对话框。到目前为止,我发现我可以使用线程/处理程序执行此操作,但不起作用,然后我发现了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_stanica AssycTask类看起来像这样:

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方法中获取“传递”数组列表的元素(并在那里使用它们),以及如何返回要在main方法中使用的数组列表(“结果“数组列表”?

5 个答案:

答案 0 :(得分:64)

将您的方法更改为:

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

UPD:

如果您想要访问任务开始上下文,最简单的方法是覆盖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);

答案 1 :(得分:12)

为什么要传递一个ArrayList? 应该可以直接用params调用execute:

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

varrargs是如何工作的,对吧? 使ArrayList传递变量是双重工作。

答案 2 :(得分: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
        }
}

答案 3 :(得分:1)

我不这样做。我发现重载asychtask类的构造函数更容易..

public class calc_stanica扩展了AsyncTask&gt;

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.*/   ...  ...  

答案 4 :(得分: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();

希望它会有所帮助。