【问题标题】:How to put method into the AsyncTask如何将方法放入 AsyncTask
【发布时间】:2014-08-09 07:16:21
【问题描述】:

你好,我有小问题。我应该如何以及在哪里将该方法放在 asyncTask 中,或者更好的是,它在 asyncTask 中的外观。

这是我的代码

public void licz()
{
     wyniki.setText("");
        N = Integer.parseInt(liczbaN.getText().toString()); 
        tablica = new String [90];
        int i=0;
            for(licz1=99;licz1>=10;licz1--)
            {
                for(licz2=10;licz2<=licz1;licz2++)
                {
                    if(N==licz1/licz2 && licz1%licz2==0)
                    {
                        String nap1 = Integer.toString(licz1);
                        String nap2 = Integer.toString(licz2);
                        napis = (nap1+ " " + nap2 +"\n");   // moj string
                        lista.add(napis);
                        tablica[i]=napis;
                        i++;    
                        wyniki.setText("");
                    }                   
                }
                String listString = "";
                for (String s : tablica)
                {
                    listString += s;
                }
                listString=listString.replaceAll("null", "");
                wyniki.setText(listString);
            }   
}

【问题讨论】:

  • 您为什么要这样做?此代码似乎不保证 AsyncTask。
  • 因为循环和数组中的值会更高
  • 这应该会有所帮助:stackoverflow.com/questions/23596903/…
  • 还有更多关于 sharedpreferences 的信息,我只想知道如何将该方法实现到 asynctask

标签: java android methods android-asynctask


【解决方案1】:
  1. 声明AsyncTask&lt;Void, String, Void&gt;
  2. 将此代码放入doInBackground()
  3. 将所有出现的wyniki.setText(whatever); 替换为publishProgress(whatever);
  4. onProgressUpdate() 中,使用wyniki.setText(progress[0])

最后两个步骤很重要,您不能从后台线程更改 UI 视图。

【讨论】:

    【解决方案2】:

    创建一个扩展 AsyncTask 的新类并覆盖其 doInBackground() 方法。将您的 licz() 方法放入其中。

    【讨论】:

      【解决方案3】:

      改变你的方法,让它返回一个字符串而不是 void

      public String licz() {
          String result = "";
          // do your fancy array stuff
          return result;
      }
      

      然后创建一个扩展AsyncTask&lt;Void, Void, String&gt; 的类并在doInBackground() 中调用您的方法并返回结果。然后使用onPostExecute() 中的结果并更新TextView:

      private class MyTask extends AsyncTask<Void, Void, String> {
          @Override   
          protected String doInBackground(Void... params) {
              return licz();
          }
      
          @Override
          protected void onPostExecute(String result) {
              wyniki.setText(result);
          }
      }
      

      【讨论】:

        【解决方案4】:

        实现AsyncTask 类并覆盖其方法:

        private class MyTask extends AsyncTask<String, Void, String> {
        @Override
            protected String doInBackground(String... params) {
                //Put your code here
        
                return "Executed";
            }
        
            @Override
            protected void onPostExecute(String result) {
                //do something after background execution is complete
                //Executes on main thread
            }
        
            @Override
            protected void onPreExecute() {
                 //do something before background execution starts
                //Executes on main thread
            }
        }  
        

        致电new MyTask().execute("");

        注意:
        更新 onPostExecute 中的 UI 相关更改。

        【讨论】:

        • 但是,如果我在其他类中拥有所有布局,它将如何工作。当我只移动方法时,它会崩溃很多
        • 您可以在onPostExecute 中进行操作。将布局变量作为类变量。拨打wyniki.setText("");onPostExecute
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多