我试图通过bundle
将HashMap
传递给另一个fragment
。然而,我的bundle
返回null
。
我用这个
创建bundle
protected void onPostExecute(String string) {
// dismiss the dialog
pDialog.dismiss();
int a = 0;
Bundle bundle = new Bundle();
Fragment fragment = new Assessment_Fragment();
for (int i = 0; i < infoList.size(); i++) {
// get HashMap
HashMap<String, String> map = infoList.get(i);
// autoincremented key for retreiving as many HashMaps as needed
bundle.putSerializable("" + a, map);
// so i will know how many hashmaps exist
bundle.putInt("key", a);
// increment key
a++;
}
fragment.setArguments(bundle);
};
这就是我接收bundle
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_assessment, container, false);
// check for values
if (getArguments() != null) {
// application never gets here !
int a = getArguments().getInt("key");
for (int i = 0; i < a; i++) {
@SuppressWarnings("unchecked")
HashMap<String, String> map = (HashMap<String, String>) getArguments()
.getSerializable("" + i);
if (map.get(TAG_FIELD).equals(r)) {...
应用程序没有崩溃,没有错误。这个fragment
永远不会膨胀,因为bundle
永远不会是!= null
。我做错了什么?
在onPostExecute()
中,您必须通过FragmentTransaction替换或添加片段:
Fragment newFragment = CountingFragment.newInstance(mStackLevel);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.simple_fragment, fragment).commit();
还可以避免迭代infoList来获取和放置每个HashMap到bundle中。你可以直接输入infoList