2014-05-11 4 views
1

을 변경할 수 있지만마다 실행 나는 탭을 전환하고 내가 다시로드 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); 
     } 

    } 
+0

당신은 우리에게 AsyncTask 클래스 – NoXSaeeD

답변

0

이 거 당신을 위해 유용 할 수 있다면 난 모르지만 아마 거 유용 할 무언가 클래스 AsyncTask를 : 글로벌 VAR로 메이크업의 JSON 객체

boolean IsJsonObjectLoaded = false; 
JSONObject jsonObj = null; 

AsyncTask를의 중첩 방법

odInBackgroud(object){ 
    // do not write anything here 
    if(!IsJsonObjectLoaded){ 

    your all code here .... 



    jsonObj = new JSONObject(jsonStr); 
    IsJsonObjectLoaded = true; 
    } 
} 

json 개체를 다음과 같이 만듭니다. 글로벌 VAR

과 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; 
} 

도움이 되었기를 바랍니다.

+0

을 보여줄 수 있습니까?하지만 doInBackground를 어디에서 대체해야합니까? –

+0

그래서 얻었습니까? 아니요? – NoXSaeeD

+0

난 잠겨있어, 아니 어디에 넣어 방법을 넣어 –

0

onPostExecute 메소드에서 true로 설정된 부울을 설정하면됩니다. 이 플래그를 사용하여 AsyncTask를 실행해야하는지 여부를 판별하십시오.

+0

나는 그가 전환 할 때 json 객체를 잃는 것 같아요! – NoXSaeeD