1

我將ID形式FirstFragment傳遞給SecondFragment。我需要,我會發送POST請求到Web服務來檢索JSON,我需要創建SecondFragment的內容。 SecondFragment是帶有自定義佈局的ListView。我正在使用自定義適配器類(MyAdapter)添加該佈局。對於JSON我也有AsyncTask類(GetCategoryTas)問題是在服務給我返回結果之前創建了SecondFragment。所以內容總是空白。我怎樣才能解決這個問題。AsyncTask和JSON - Android

FirstFragment:

@Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

    Bundle bundle = new Bundle(); 
    bundle.putString("id", menuList.get(position).getAlias()); 
    categoryPage = new FragmentCategoryPage(); 
    categoryPage.setArguments(bundle); 
    transaction = manager.beginTransaction(); 
    transaction.replace(R.id.mainContainer, categoryPage, "CategoryPage"); 
    transaction.addToBackStack("CategoryPage"); 
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 
    transaction.commit(); 

} 

SecondFragment:

public class FragmentCategoryPage extends Fragment implements OnItemClickListener { 

    private ListView lvCategory; 
    private List<NameValuePair> pair; 

    private ArrayList<ItemCategory> itemCatList = new ArrayList<ItemCategory>(); 
    private ItemCategory itemCat; 

    private MyAdapter myAdapter; 
    private Context context; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_category, container, false); 

     context = getActivity(); 

     String id = getArguments().getString("id"); 
     pair = new ArrayList<NameValuePair>(); 
     pair.add(new BasicNameValuePair("category", id)); 
     new GetCategoryTask().execute(); 

     lvCategory = (ListView) view.findViewById(R.id.lvCategory); 

     myAdapter = new MyAdapter(); 
     lvCategory.setAdapter(myAdapter); 
     lvCategory.setOnItemClickListener(this); 

     return view; 
    } 

    // Creating own adapter for categories 
    class MyAdapter extends BaseAdapter { 

     @Override 
     public View getView(final int pos, View convertView, ViewGroup parent) { 
      View row = null; 

      if (convertView == null) { 
       LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       row = inflater.inflate(R.layout.item_layout_category, parent, false); 
      } else { 
       row = convertView; 
      } 

      TextView tvCatTitle = (TextView) row.findViewById(R.id.tvCategoryTitle); 
      TextView tvCatDate = (TextView) row.findViewById(R.id.tvCategoryDate); 
      ImageView ivCatImage = (ImageView) row.findViewById(R.id.ivCategoryImage); 

      return row; 
     } 

     @Override 
     public int getCount() { return itemCatList.size(); } 
     @Override 
     public Object getItem(int pos) { return null; } 
     @Override 
     public long getItemId(int pos) { return 0; } 
    } 

    public class GetCategoryTask extends AsyncTask<String, Void, Boolean> { 

     @Override 
     protected Boolean doInBackground(String... params) { 

      try { 
       DefaultHttpClient httpClient = new DefaultHttpClient(); 
       HttpPost httpPost = new HttpPost(Variables.URL_CATEGORY); 
       httpPost.setEntity(new UrlEncodedFormEntity(pair)); 

       HttpResponse httpResponse = httpClient.execute(httpPost); 
       HttpEntity httpEntity = httpResponse.getEntity(); 
       String data = EntityUtils.toString(httpEntity); 

       int status = httpResponse.getStatusLine().getStatusCode(); 

       if (status == HttpStatus.SC_OK) { 

        JSONArray jArray = new JSONArray(data); 
        for (int i = 0; i < jArray.length(); i++) { 

         JSONObject jRealObject = jArray.getJSONObject(i); 

         itemCat = new ItemCategory(); 
         itemCat.setId(jRealObject.get("id").toString()); 
         itemCat.setTitle(jRealObject.get("title").toString()); 
         itemCat.setImage_name(jRealObject.get("image_name").toString()); 

         itemCatList.add(itemCat); 
        } 

        return true; 
       } 
      } catch (UnsupportedEncodingException e) { 
       e.printStackTrace(); 
      } catch (ClientProtocolException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      return false; 
     } 

     @Override 
     protected void onPostExecute(Boolean result) { 
      super.onPostExecute(result); 

      if(result == false) { 
       Toast.makeText(context, Variables.ERROR_MESSAGE, Toast.LENGTH_SHORT).show(); 
      } 
     } 
    } 
} 

我怎樣才能解決這個問題呢?

回答

2

您應該通過調用刷新您的適配器onPostExecute();

myAdapter.notifyDataSetChanged(); 
+0

Oooooooooooooooooooooooooooooo人謝謝你能肯定的是,名單中的該點存在非常!!! :D我被卡住了整天tihs sh * t :)再次感謝! – KiKo 2014-09-25 21:54:39

0

我沒有看到你的代碼中的任何地方,你已經連接你的listView與itemCatList。你應該做任何你想做的事與onPostExecute你的列表視圖,而不是在的onCreate,這樣的AsyncTask在onPostExecute在自己的線程 運行

lv = (ListView) findViewById(R.id.your_list_view_id); 
List<String> your_array_list = new ArrayList<String>(); 
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
     this, 
     android.R.layout.simple_list_item_1, 
     your_array_list); 

lv.setAdapter(arrayAdapter); 
+0

我有自定義佈局的自定義適配器,我在那裏:) – KiKo 2014-09-25 21:56:20