2015-04-26 167 views
0

以下是我正在嘗試執行的操作:從遠程服務器獲取JSON數組並在列表視圖中呈現它(在新活動中)當從彈出框中的項目列表中選擇項目時。彈出窗口是在點擊/點擊活動中的某個元素時創建的。更具體地說:一項活動顯示網格視圖中的類別列表。每個類別都有一些子類別,當用戶點擊某個類別時,它們會顯示爲彈出式菜單中的項目列表。當用戶選擇彈出式菜單中的一個項目時,會創建一個新的活動,顯示該子類別中的項目列表。項目詳細信息作爲JSON數組從遠程服務器獲取。 我已經創建了一個AsyncTask從彈出窗口中選擇子類別時從服務器獲取項目詳細信息。以下是此代碼段。當從嵌套事件偵聽器執行AsyncTask時,onPostExecute()未被調用

public class Section extends Fragment { 
    ... 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     ... 

     mGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      public void onItemClick(AdapterView <? > parent, final View v, nt position, long id) { 
       ... 

       PopupMenu popup = new PopupMenu(getActivity(), v); 

       ... 

       popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { 
        public boolean onMenuItemClick(MenuItem item) { 
         new GetProductDataFromServer_Task().execute(productURL); 
         Intent intent = new Intent(getActivity(), ProductListScreen.class); 
         startActivity(intent); 

         return true; 
        } 
       }); 
      }); 
     } 

     return rootView; 
    } 
} 

所述片段是用於顯示在網格佈局中的類別列表的活動的一部分。

而且,這裏是從服務器獲取JSONArray的AsyncTask。

private class GetProductDataFromServer_Task extends AsyncTask < String, Long, JSONObject > { 

    @Override 
    protected JSONObject doInBackground(String...params) { 
     HttpEntity httpEntity = null; 

     HttpGet httpGet = null; 
     try { 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      httpGet = new HttpGet(params[0]); 
      HttpResponse httpResponse = httpClient.execute(httpGet); 
      httpEntity = httpResponse.getEntity(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     // Convert HttpEntity into JSON Array 
     JSONObject jsonObject = null; 

     if (httpEntity != null) { 
      try { 
       String entityResponse = EntityUtils.toString(httpEntity); 
       jsonObject = new JSONObject(entityResponse); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (IllegalStateException e) { 
       e.printStackTrace(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
     return jsonObject; 
    } 

    @Override 
    protected void onPostExecute(JSONObject jsonObject) { 
     try { 
      JSONArray products = jsonObject.getJSONArray("products"); 
      int numProducts = products.length(); 
      mProductList = new ProductInfoForGridView[numProducts]; 
      for (int prodIdx = 0; prodIdx < numProducts; prodIdx++) { 
       mProductList[prodIdx] = GetProductObjFromJSONObj(products.getJSONObject(prodIdx)); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

我面臨的問題是onPostExecute()由於某種原因沒有被調用。 JSONArray成功從服務器獲取,doInBackground()函數完成其執行,沒有任何例外。我懷疑這與嵌套事件偵聽器有關,因爲如果我從類別偵聽器(即在onItemClick()中調用嵌套子類別偵聽器setOnMenuItemClickListener())的函數啓動Async任務,它工作得很好。

在此先感謝您的回答。我試圖在我的問題中描述。如果需要,我會很樂意提供更多細節。

+0

您是否正在從主(UI)線程執行asynctask?否則onPostExecute將不會被調用。 –

+0

這裏是導航流程:啓動屏幕 - >類別屏幕(實現水平分頁 - 創建併爲每個選項卡分配一個片段) - >片段:onCreateView - > mGrid.setOnItemClickListener:onItemClick() - > popup.setOnMenuItemClickListener:onMenuItemClick() - >(new GetProductDataFromServer_Task()。execute(productURL);)。我的理解是,導致創建AsyncTask的所有活動都發生在UI線程上。如果我錯了,請糾正我。 –

+0

更新...當我在AsynTask的開始和使用AsyncTask從遠程服務器獲取的數據的開始之間引入大約2秒的延遲時,我沒有看到之前報告的問題。我從這種行爲推斷出,網絡延遲在防止Android框架調用onPostExecute()方面發揮了一定的作用。我打算處理這個問題的方式是顯示一條「加載...」消息,直到我收到所有數據,然後開始新的活動。如果有人能提出更好的解決方案,我將不勝感激。謝謝 –

回答

0

您確定onPostExecute未運行。您是否嘗試過

yourGridAdapter.notifyDataSetChanged() in onPostExecute

相關問題