1

ID 양식 FirstFragment를 SecondFragment로 전달 중입니다. SecondFragment의 내용을 만들어야하는 JSON을 검색하기 위해 웹 서비스에 POST 요청을 보내야합니다. 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 남자 주셔서 감사합니다 AsyncTask를이 onPostExecute에서 자체 스레드 에서 실행되는대로 목록이 그 지점에 있는지 확신 할 수 너무 많이 !!! : D 나는 tihs sh * t 하루 종일 붙어 있었다 :) 다시 한번 감사드립니다! – KiKo

0

코드에서 listview를 itemCatList와 연결 한 곳을 볼 수 없습니다. 그리고 당신은 당신이하지에서 onCreate에서 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