有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

package x;

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

//import 安卓.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();

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

                return false;
            }
        });

    }

}

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

我的第二个片段是:

package x;

import 安卓.os.Bundle;
import 安卓.support.v4.app.Fragment;
import 安卓.util.Log;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.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;
    }
}

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

那么这里可能有什么问题呢


共 (1) 个答案

  1. # 1 楼答案

    改变

                fragment.setArguments(bundle);
    

                eventDetailsFragment.setArguments(bundle);
    

    因为您正在为fragment设置bundle,但在fragTransaction.replace中传递eventDetailsFragment实例