在我的onCreateView中定义了AsyncTask的执行,但当我切换标签并返回到运行此AsyncTask运行重载json请求的选项卡时
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View android = inflater.inflate(R.layout.android_frag, container, false);
contactList = new ArrayList<HashMap<String, String>>();
new GetContacts().execute();
return android;
}
因为我可以在第一次使用该应用程序时让它继续运行吗?
类AsyncTask
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Espere, cargando datos...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Conseguir nodo matriz JSON
categories = jsonObj.getJSONArray(TAG_CONTACTS);
// bucle a través de todos las categorias
for (int i = 0; i < categories.length(); i++) {
JSONObject c = categories.getJSONObject(i);
String id = c.getString(TAG_ID);
String title = c.getString(TAG_title);
String slug = c.getString(TAG_slug);
String desc = c.getString(TAG_description);
// tmp hashmap para las ventanillas únicas
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put(TAG_ID, id);
contact.put(TAG_title, title);
contact.put(TAG_slug, slug);
contact.put(TAG_description, desc);
// añadir categoria a la lista de categorias
contactList.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "no pudo obtener ningún dato de la url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Actualización de datos analizados JSON en ListView
* */
ListView vista = (ListView) getActivity().findViewById(R.id.list);
ListAdapter adapter = new SimpleAdapter(
getActivity(), contactList,
R.layout.list_item, new String[]{TAG_title}, new int[]{R.id.name}
);
vista.setAdapter(adapter);
}
}
答案 0 :(得分:0)
我不知道这对你有用,但这里有些东西可能会有用: 将json对象设为全局变量
boolean IsJsonObjectLoaded = false;
JSONObject jsonObj = null;
在odInBackgroud方法的AsyncTask中
odInBackgroud(object){
// do not write anything here
if(!IsJsonObjectLoaded){
your all code here ....
jsonObj = new JSONObject(jsonStr);
IsJsonObjectLoaded = true;
}
}
将json对象设为全局变量
并在onCreateView方法中:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View android = inflater.inflate(R.layout.android_frag, container, false);
// you code here ...
if(!IsJsonObjectLoaded){
new GetContacts().execute();
)
return android;
}
我希望这会有所帮助。
答案 1 :(得分:0)
只需设置一个标志,即在onPostExecute方法中设置为true的布尔值。使用此标志来确定是否应该运行AsyncTask。