我正在開發一個AsyncTask類。我試圖解析Json並展示它。一切都很好,但我有一個問題。我想檢查我的Asynctask類的狀態。我的意思是我想檢查AsyncTask何時完成。Android Asynctask檢查狀態(已完成)
這是我的代碼:
private class LoadItemFromServer extends AsyncTask<String, Void, String> {
ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
try {
JsonResult = FlashScreen
.execute(
"***************",
FlashScreen.CreateJson(
"GetCategoryHierharchy", "eng"));
} catch (Exception e) {
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
Log.e("serverrrr", JsonResult);
try {
ParseJsonFromServer(JsonResult);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void ParseJsonFromServer(String server) throws JSONException {
JSONArray jsonArray = null;
JSONObject mainJson = new JSONObject(server);
jsonArray = mainJson.getJSONArray("result");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject data = jsonArray.getJSONObject(i).getJSONObject("data");
Azercell_PagerTitleStrip_items objItem = new Azercell_PagerTitleStrip_items();
objItem.setTitle(data.getString("title"));
Log.e("titlesss", data.getString("title"));
arrayOfList.add(objItem);
}
}
我的問題是關於這一部分:
loaditem = new LoadItemFromServer();
loaditem.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
try {
if (loaditem.getStatus() == AsyncTask.Status.FINISHED) {
for (int i = 0; i < arrayOfList.size(); i++) {
page = inflater.inflate(R.layout.azercell_category_pages, null);
// setAdapterToListview(page);
titles.add(arrayOfList.get(i).getTitle());
pages.add(page);
}
} else {
Log.e("asynctask status", "not finished");
}
} catch (Exception e) {
}
此刻我arrayOfList
數組的大小爲0,但我登錄ParseJsonFromServer和數組的大小不是0. 我不能等待,AsyncTask何時完成? 我該如何解決我的問題?如果有人知道一個解決方案,請大家幫我
謝謝你工作完美 – BekaKK 2014-08-29 09:46:25