gpt4 book ai didi

android - 如何使用获取/设置参数将对象形式的 Tabhost Activity 传递给选项卡 fragment

转载 作者:行者123 更新时间:2023-11-30 01:32:50 26 4
gpt4 key购买 nike

这里我有带有四个 fragment 选项卡的 tabhost Activity 。在这里,我必须将此 tabhost Activity 中的对象传递给所有 fragment 选项卡。为此我正在使用获取/设置参数,但是当我试图获取 fragment 中的对象时它会抛出空值。

这是我的tabhostAtivity

 @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_host);
tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
context = getApplicationContext();

final InitCollegeMO initCollegeMO = (InitCollegeMO) getIntent().getSerializableExtra("initCollegeMO");
Log.e("tab", "init" + initCollegeMO);
Bundle bundle = new Bundle();
bundle.putSerializable("collegedetails", initCollegeMO);
AboutCollegeFragment fragment = new AboutCollegeFragment();
fragment.setArguments(bundle);
tabHost.addTab(tabHost.newTabSpec(TAB_1_TAG).setIndicator("", getResources().getDrawable(R.drawable.college)), AboutCollegeFragment.class, null);
tabHost.addTab(tabHost.newTabSpec(TAB_2_TAG).setIndicator("", getResources().getDrawable(R.drawable.course)), CourseFragment.class, null);
tabHost.addTab(tabHost.newTabSpec(TAB_3_TAG).setIndicator("", getResources().getDrawable(R.drawable.admission)), AdmissionFragment.class, null);
tabHost.addTab(tabHost.newTabSpec(TAB_4_TAG).setIndicator("", getResources().getDrawable(R.drawable.contact)), ContactFragment.class, null);
tabHost.getTabWidget().setCurrentTab(0);


}
}

这是我的aboutCollegeFragment

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//here i got null value
activeInitCollegMO = (InitCollegeMO) getArguments().getSerializable("initCollegeMO");
Log.e("tab", "init" + activeInitCollegMO);
view = inflater.inflate(R.layout.web_view, container, false);
webView = (WebView) view.findViewById(R.id.activity_tab_host_webview);
return view;

}

最佳答案

使用此代码发送对象

 Bundle bundle = new Bundle();
bundle.putString("fromFragment", new Gson().toJson(myData ));
MyFragment myFragment = new MyFragment ();
myFragment .setArguments(bundle);
replaceFragment(context, myFragment );

获取下一个 fragment 的值(value)

 bundle = this.getArguments();
if (bundle != null) {
String json = bundle.getString("fromFragment", "");
Type dataType = new TypeToken<MyData >() {
}.getType();
MyData myData = new Gson().fromJson(json, dataType);

}

关于android - 如何使用获取/设置参数将对象形式的 Tabhost Activity 传递给选项卡 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35521578/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com