<small id='d6my6'></small><noframes id='d6my6'>

  • <i id='d6my6'><tr id='d6my6'><dt id='d6my6'><q id='d6my6'><span id='d6my6'><b id='d6my6'><form id='d6my6'><ins id='d6my6'></ins><ul id='d6my6'></ul><sub id='d6my6'></sub></form><legend id='d6my6'></legend><bdo id='d6my6'><pre id='d6my6'><center id='d6my6'></center></pre></bdo></b><th id='d6my6'></th></span></q></dt></tr></i><div id='d6my6'><tfoot id='d6my6'></tfoot><dl id='d6my6'><fieldset id='d6my6'></fieldset></dl></div>
    <tfoot id='d6my6'></tfoot>

      <bdo id='d6my6'></bdo><ul id='d6my6'></ul>
    1. <legend id='d6my6'><style id='d6my6'><dir id='d6my6'><q id='d6my6'></q></dir></style></legend>

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

        时间:2023-10-04

          <legend id='0Q3wb'><style id='0Q3wb'><dir id='0Q3wb'><q id='0Q3wb'></q></dir></style></legend>

          1. <i id='0Q3wb'><tr id='0Q3wb'><dt id='0Q3wb'><q id='0Q3wb'><span id='0Q3wb'><b id='0Q3wb'><form id='0Q3wb'><ins id='0Q3wb'></ins><ul id='0Q3wb'></ul><sub id='0Q3wb'></sub></form><legend id='0Q3wb'></legend><bdo id='0Q3wb'><pre id='0Q3wb'><center id='0Q3wb'></center></pre></bdo></b><th id='0Q3wb'></th></span></q></dt></tr></i><div id='0Q3wb'><tfoot id='0Q3wb'></tfoot><dl id='0Q3wb'><fieldset id='0Q3wb'></fieldset></dl></div>
              <tbody id='0Q3wb'></tbody>

            <small id='0Q3wb'></small><noframes id='0Q3wb'>

              <bdo id='0Q3wb'></bdo><ul id='0Q3wb'></ul>
                <tfoot id='0Q3wb'></tfoot>

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

                  问题描述

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

                  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<String> 传递给 AsyncTask (并在那里使用它),以及如何取回一个新的 ArrayList<String> 喜欢 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<String> to the AsyncTask (and work with it there), and how to get back a new ArrayList<String> like a result from the AsyncTask?

                  该方法的代码如下所示:

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

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

                  推荐答案

                  把你的方法改成这样:

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

                  更新:

                  如果您想访问任务启动上下文,最简单的方法是就地覆盖 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,并返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:快速从函数中返回多个值 下一篇:我想在执行 firebase 代码后返回布尔值

                  相关文章

                  <tfoot id='e7o3s'></tfoot>
                  <i id='e7o3s'><tr id='e7o3s'><dt id='e7o3s'><q id='e7o3s'><span id='e7o3s'><b id='e7o3s'><form id='e7o3s'><ins id='e7o3s'></ins><ul id='e7o3s'></ul><sub id='e7o3s'></sub></form><legend id='e7o3s'></legend><bdo id='e7o3s'><pre id='e7o3s'><center id='e7o3s'></center></pre></bdo></b><th id='e7o3s'></th></span></q></dt></tr></i><div id='e7o3s'><tfoot id='e7o3s'></tfoot><dl id='e7o3s'><fieldset id='e7o3s'></fieldset></dl></div>

                      <bdo id='e7o3s'></bdo><ul id='e7o3s'></ul>
                  1. <small id='e7o3s'></small><noframes id='e7o3s'>

                    <legend id='e7o3s'><style id='e7o3s'><dir id='e7o3s'><q id='e7o3s'></q></dir></style></legend>