0

我正在尝试将 a 传递HashMap给另一个fragmentvia bundle。不过我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根本不会膨胀,因为bundleis never != null。我究竟做错了什么?

4

1 回答 1

1

onPostExecute()您必须通过 FragmentTransaction 替换或添加片段:

 Fragment newFragment = CountingFragment.newInstance(mStackLevel);
 FragmentTransaction ft = getFragmentManager().beginTransaction();
 ft.add(R.id.simple_fragment, fragment).commit();

您也可以避免遍历您的 infoList 以获取并将每个 HashMap 放入 bunde 中。你可以直接把infoList

于 2013-06-07T19:37:08.593 回答