使用bundle返回null传递HashMap

时间:2013-06-07 19:16:09

标签: android android-fragments bundle

我正在尝试通过HashMapfragment传递给另一个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永远不会bundle,因此!= null永远不会膨胀。我做错了什么?

1 个答案:

答案 0 :(得分:1)

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

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

你也可以避免迭代你的infoList来获取并将每个HashMap放在bunde中。你可以直接放入infoList