2014-05-11 51 views
1

在我的課已經definica的AsyncTask在我onCreateView執行,但是當我切換標籤,然後回來的標籤,讓我跑這個的AsyncTask運行重載JSON請求的AsyncTask運行,每次更改標籤

@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

​​
+0

你可以告訴我們AsyncTask類 – NoXSaeeD

回答

0

我不知道這是否會是對您有用,但這裏的東西也許會是有用的: 使JSON對象作爲全局變量

boolean IsJsonObjectLoaded = false; 
JSONObject jsonObj = null; 

中的AsyncTask in odInBackgroud方法

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

我希望這有助於。

+0

好吧,但我應該在哪裏具體doInBackground替換? –

+0

你是這麼認爲的嗎? – NoXSaeeD

+0

我保持鎖定,沒有在哪裏把方法 –

0

只需設置一個標誌,即在onPostExecute方法中設置爲true的布爾值。使用此標誌來確定是否應該運行AsyncTask。

+0

我認爲他在切換時丟失了json對象! – NoXSaeeD