传递参数的AsyncTask,并返回结果 [英] Passing arguments to AsyncTask, and returning results

查看:121
本文介绍了传递参数的AsyncTask,并返回结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这应该是很容易回答的问题。我有做一些长期计算的应用程序,我想显示一个进度对话框,而做到这一点。到目前为止,我发现我可以使用线程/处理器做到这一点,但没有工作,然后我发现了关于AsyncTask的。

I think this should be easy to answer. I have an application that does some long calculations, and I would like to show a progress dialog while this is done. So far I have found that I could do this with threads/handlers, but didn't work, and then I found out about the AsyncTask.

在我的应用程序使用的地图,上面有标记,而我已经实现和Ontap函数来调用,我已经定义的方法。该方法将创建是/否按钮的对话框,我想打电话给一个AsyncTask的,如果是的点击。我的问题是如何通过一个ArrayList的AsyncTask的(和与之有合作),以及如何回到一个新的ArrayList像是从AsyncTask的?

In my application I use maps with markers on it, and I have implemented the onTap function to call a method that I have defined. The method creates a dialog with Yes/No buttons, and I would like to call an AsyncTask if Yes is clicked. My question is how to pass an ArrayList to the AsyncTask (and work with it there), and how to get back a new ArrayList like a result from the AsyncTask?

该方法的code是这样的:

The code of the method looks like this:

                       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类看起来是这样的:

So, as you see, I would like to send the string array list "passing" to the AsyncTask, and to get the "result" string array list back from it. And the calc_stanica AssycTask class looks like this:

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方法的元素(并使用它们),以及如何返回一个数组列表中的主要方法使用(在结果的数组表)?

So my question is how to get the elements of the "passing" array list in the AsyncTask doInBackground method (and use them there), and how to return an array list to use in the main method (the "result" array list)?

感谢你在前进......

Thank you in advance...

博扬Ilievski

Bojan Ilievski

推荐答案

更​​改方法是这样的:

Change your method to look like this:

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

和改变你的异步任务执行

And change your async task implementation

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的地方:

If you want to have access to the task starting context, the easiest way would be to override onPostExecute in place:

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

这篇关于传递参数的AsyncTask,并返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆