i_me_mine
i_me_mine

Reputation: 1433

passing HashMap with bundle returning null

I am attempting to pass a HashMap to another fragment via bundle. However my bundle is returning null.

I create the bundle with this

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);
    };

This is how I receive the 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)) {...

The application doesn't crash, there are no errors. This fragment simply never inflates because the bundle is never != null. What am I doing wrong?

Upvotes: 0

Views: 1866

Answers (1)

Blackbelt
Blackbelt

Reputation: 157457

In the onPostExecute() you have to replace or add the fragment through a FragmentTransaction:

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

also you can avoid to iterate through your infoList to get and put every HashMap inside the bunde. You can put directly infoList

Upvotes: 1

Related Questions