38

我有一个执行一些长时间计算的应用程序,我想在完成时显示一个进度对话框。到目前为止,我发现我可以使用线程/处理程序来做到这一点,但是没有用,然后我发现了AsyncTask.

在我的应用程序中,我使用带有标记的地图,并且我已经实现了 onTap 函数来调用我定义的方法。AsyncTask该方法创建一个带有是/否按钮的对话框,如果单击是,我想调用一个。我的问题是如何将 an 传递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?应该可以直接使用参数调用执行:

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

这就是 varrargs 的工作方式,对吧?制作一个 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 类的构造函数更容易..

公共类 calc_stanica 扩展 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

您可以收到这样的返回结果: AsyncTaskclass

@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 回答