我在我的应用程序中使用 fragment 。这是我第一个简单地扩充 xml 文件的 fragment :
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,其中包含一个 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 类:
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");
}
}
我不知道如何将 Bundle arguments
从 FragmentA 传递到 ListFrag。
最佳答案
在您的 FragmentA fragment 中,将 bundle 设置为参数。
Bundle args = new Bundle();
args.putInt("doctor_id",value);
ListFrag newFragment = new ListFrag ();
newFragment.setArguments(args);
在您的 ListFrag fragment 中获取 bundle
Bundle b = getArguments();
int s = b.getInt("doctor_id");
关于android - 如何将 Bundle 从 Fragment 传递到 Fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17063378/