2010-11-16 64 views
34

我有一个应用程序做了一些长时间的计算,并且我希望在完成时显示进度对话框。到目前为止,我发现我可以用线程/处理程序来完成这个工作,但是没有工作,然后我发现AsyncTask将参数传递给AsyncTask,并返回结果

在我的应用程序中,我使用带有标记的贴图,并且实现了onTap函数来调用我定义的方法。该方法使用“是/否”按钮创建对话框,如果单击“是”,我想调用AsyncTask。我的问题是如何通过ArrayList<String>AsyncTask(并在那里工作),以及如何从AsyncTask得到一个新的ArrayList<String>

方法的代码如下所示:

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

回答

61

改变你的方法是这样的:

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

谢谢你的回答,但我还有一件事要问。如果我定义int min = Integer.parseInt(minim);例如,在AsyncTask类onPostExecute()中,我如何从我的主类方法访问它?当我像这样改变它时,在主类方法中出现“min不能解决”错误。 – 2010-11-16 16:35:18

+2

@Bojan Ilievski:让你的min变量全局。 – Wroclai 2010-11-16 16:55:25

+0

再次感谢您的答案,我已经解决了我的问题... – 2010-11-16 21:18:31

12

为什么要传递一个ArrayList? 它应该有可能只是调用直接与PARAMS执行:

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

那怎么varrargs工作,对不对? 使ArrayList传递变量是双重工作。

+0

我非常赞同Leander在这里。无论如何改变正确的答案复选标记? – 2012-10-23 21:57:40

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 
     } 
} 
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.*/ ... ... 
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(); 

希望我t会有所帮助。

相关问题