不要因爲這個例子
執行任務
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來改變視圖。
希望它有助於!