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

标签 java android android-listview android-fragments

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

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

    }

}

现在,在子单击事件中,我想将子值传递给另一个 fragment 。

我的第二个 fragment 是:

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

在这里,即使第一个 fragment 中的 bundle 中的值已正确存储,我也无法接收 bundle 中存储的值。

那么这里可能存在什么问题?

最佳答案

改变

            fragment.setArguments(bundle);

            eventDetailsFragment.setArguments(bundle);

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

关于java - 无法使用 Bundle 将字符串从一个 fragment 传递到另一个 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21801401/

相关文章:

android - gmail android App 如何加载它的 ListView?

android - 将 Listview 嵌入到 Android 的首选项中

java - 没有 else 语句的 if 语句不好吗?

java - 需要清理 Java 8 中的 ResponseEntity 主体

java - Actor 的输入监听器

java - 无法使用 JDBC 运行 java 程序来连接 postgres?

android - 在 Google map MapView 中显示船只路线 - Android

android - 来自 ADT21 的 UI 测试工具 android

java - 只允许我的 android 应用程序在 java 中执行端点 api

java - Android ListView 从数据库记录实现的问题