2013-07-27 48 views

回答

0

不要因爲這個例子

執行任務

new LongOperation().execute(""); 

,任務是這樣的。

private class LongOperation extends AsyncTask<String, Void, String> { 

      @Override 
      protected String doInBackground(String... params) { 
       for(int i=0;i<5;i++) { 
        try { 
         Thread.sleep(1000); 
        } catch (InterruptedException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 

       return "Executed"; 
      }  

      @Override 
      protected void onPostExecute(String result) { 
       TextView txt = (TextView) findViewById(R.id.output); 
       txt.setText("Executed"); // txt.setText(result); 
       //might want to change "executed" for the returned string passed into onPostExecute() but that is upto you 
      } 

      @Override 
      protected void onPreExecute() { 
      } 

      @Override 
      protected void onProgressUpdate(Void... values) { 
      } 
    } 

DoinBackground方法是您從服務器上下載數據的地方。並在後期將該數據分配給基礎適配器。並且介意一件事是你不能從DoinBackground方法更新視圖。你可以使用UIThread來改變視圖。

希望它有助於!

0

最好的方法是在創建適配器並將其設置爲ListView之前,在AsyncTask中下載數據。

但無論如何,如果你需要下載數據時,已經創建了ListView控件,那麼你作爲@Arman陌生人說你需要實現內部doInbackgound()所有的工作職能前。

F.e.當你下載圖片時,你應該調整它的大小,並執行notifyDataSetChanged()來重繪你的列表視圖。

相關問題