如何从片段通过捆绑到碎片 [英] How to pass Bundle from Fragment to Fragment

查看:131
本文介绍了如何从片段通过捆绑到碎片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用我的应用程序片段。这是我简单地夸大一个XML文件中第一个片段:

I am using fragments in my application. This is my first fragment that simply inflate a xml file:

public class FragmentA extends SherlockFragment
{
    Context myContext,appContext;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    myContext = getActivity();
    appContext=getActivity().getApplicationContext();
    arguments = getArguments();
    doctor_id=arguments.getInt("doctor_id");
    userType=arguments.getString("userType");
    return inflater.inflate(R.layout.left_panel, container,false);
}

和这是left_panel的.xml,它包含一个片段:

and this is the left_panel .xml, that contains a fragment:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <fragment
        android:id="@+id/titles"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        class="com.example.sample.ListFrag" />

</LinearLayout>

这是我的ListFrag类:

This is my ListFrag class:

public class ListFrag extends Fragment 
{
    Context myContext,appContext;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        layoutView = inflater.inflate(R.layout.activity_doctor_list,container);
        myContext = getActivity();
        appContext=getActivity().getApplicationContext();
        arguments=getArguments();
        int doctor_id=arguments.getInt("doctor_id");
}
}

我不知道怎么传捆绑参数从碎裂到ListFrag。

I don't know how to pass Bundle arguments from FragmentA to ListFrag.

推荐答案

在您的碎裂片段设置捆绑作为参数。<​​/ P>

In your FragmentA fragment set the bundle as the argument.

Bundle args = new Bundle();
args.putInt("doctor_id",value);    
ListFrag newFragment = new ListFrag ();
newFragment.setArguments(args);

在您ListFrag片段让包作为

In your ListFrag fragment get the bundle as

Bundle b = getArguments();
String s = b.getInt("doctor_id");

这篇关于如何从片段通过捆绑到碎片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆