我正在将ID形式从FirstFragment传递给SecondFragment。我需要将POST请求发送到Web服务以检索JSON,这需要创建SecondFragment的内容。 SecondFragment是具有自定义布局的ListView。我正在使用我的自定义适配器类(MyAdapter)添加该布局。对于JSON,我也有AsyncTask类(GetCategoryTas)问题是在服务将结果返回给我之前,创建SecondFragment。因此,内容始终为空白。我怎样才能解决这个问题。
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();
}
}
}
}
我该如何解决这个问题?
最佳答案
您应该通过调用在onPostExecute()
中刷新适配器。
myAdapter.notifyDataSetChanged();