을 변경할 수 있지만마다 실행 나는 탭을 전환하고 내가 다시로드 JSON 요청AsyncTask를 내 onCreateView에서 AsyncTask를 실행 definica 내 수업에 탭
@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);
}
}
당신은 우리에게 AsyncTask 클래스 – NoXSaeeD