无法使用Bundle将字符串从一个片段传递到另一个片段

时间:2014-02-15 17:58:37

标签: java android android-listview android-fragments

我有一个包含可扩展ListView的片段,其代码为:

package x;

import x.expandablelistadapter.ExpListAdapter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupExpandListener;

//import android.provider.DocumentsContract.Document;

public class EventFragment extends Fragment {
    private String[] categories;
    private String[][] categoryItems;
    private ExpandableListView expandableListView;
    private ExpandableListAdapter expandableListAdapter;
    private int lastExpandedGroup;

    public EventFragment() {
        this.lastExpandedGroup = -1;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // get the event categories
        categories = getResources().getStringArray(R.array.eventCategory);
        // get the computer science games
        String[] category1 = getResources().getStringArray(R.array.csEvents);
        // get the ec games
        String[] category2 = getResources().getStringArray(R.array.ecEvents);
        // get the online games
        String[] category3 = getResources().getStringArray(R.array.onEvents);
        // get the robotics games
        String[] category4 = getResources().getStringArray(R.array.rbEvents);
        // get all the category items
        categoryItems = new String[][] { category3, category1, category2,
                category4 };
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.event_layout, container,
                false);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // get the expandable list view
        expandableListView = (ExpandableListView) view
                .findViewById(R.id.eventListExp);
        expandableListAdapter = new ExpListAdapter(categories, categoryItems,
                getActivity());
        expandableListView.setAdapter(expandableListAdapter);
        expandableListView.setGroupIndicator(null);
        expandableListView
                .setOnGroupExpandListener(new OnGroupExpandListener() {

                    @Override
                    public void onGroupExpand(int groupPosition) {
                        if (groupPosition != lastExpandedGroup
                                && lastExpandedGroup != -1) {
                            expandableListView.collapseGroup(lastExpandedGroup);
                        }
                        lastExpandedGroup = groupPosition;
                    }

                });

        expandableListView.setOnChildClickListener(new OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                // TODO Auto-generated method stub

                String event = (String) expandableListAdapter.getChild(
                        groupPosition, childPosition);
                EventFragment fragment = new EventFragment();
                Bundle bundle = new Bundle();
                bundle.putString("Event", event);
                Log.d("Bundle", bundle.toString());
                fragment.setArguments(bundle);

                EventDetailsFragment eventDetailsFragment = new EventDetailsFragment();

                android.support.v4.app.FragmentTransaction fragTransaction = getFragmentManager()
                        .beginTransaction();
                fragTransaction.replace(R.id.content_frame,
                        eventDetailsFragment);
                // fragTransaction.addToBackStack(null);
                fragTransaction.commit();

                return false;
            }
        });

    }

}

现在,在子点击事件中,我想将child的值传递给另一个片段。

我的第二个片段是:

package x;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class EventDetailsFragment extends Fragment {
    String eventString;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        //Log.d("Bundleargs",""+ args.toString());
         if (args  != null && args.containsKey("Event"))
             eventString = args.getString("Event");
         else
             eventString="Nothing";
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.eventdetails_layout, container,
                false);


         TextView eventTextView = (TextView)rootView.findViewById(R.id.eventTextView);
         eventTextView.setText(eventString);

        return rootView;
    }
}

在这里,即使第一个片段中的包中的值被正确存储,我也无法接收存储在包中的值。

那么这可能是什么问题?

1 个答案:

答案 0 :(得分:1)

更改

            fragment.setArguments(bundle);

            eventDetailsFragment.setArguments(bundle);

因为您为fragment设置了包,但在eventDetailsFragment

中传递了fragTransaction.replace个实例