当我按下Bundle
中列出的某个项目时,我试图通过setArguments()
使用Fragment
从活动传递值到另一个类ListView
。
如下所示,我使用Log.i
语句来了解收到的Bundle
是否为null
。不幸的是,它总是null
,因此,在TextView
扩展Fragment
的类的指定FragmentClass
上没有数据显示。我错过了什么或代码有什么问题。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragmentclasslayout, null);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
if (getArguments() != null) {
Log.i(TAG, "getArgument is not null");
int value = getArguments().getInt("pos");
TextView tv = (TextView) getView().findViewById(R.id.tvID);
tv.setTextSize(value);
tv.setText("size = "+value*10);
}else {
Log.i(TAG, "getArgument is null");
}
}
MainClass_implements_onItemSelectedListener
setContentView(R.layout.mainactivity);
ListView mListView = (ListView) findViewById(R.id.list);
ArrayAdapter<String> mArrayAdapter = new ArrayAdapter<String>(getApplicationContext(),
R.layout.listviewlayout, R.id.tv, string);
mListView.setAdapter(mArrayAdapter);
mListView.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Fragment f = new FragmentClass();
Bundle bundle = new Bundle();
bundle.putInt("pos", (position+1));
FragmentClass fc = new FragmentClass().newInstance(bundle);
//f.setArguments(bundle);
FragmentTransaction t =
getFragmentManager().beginTransaction();
t.replace(R.id.fragment00ID, f);
t.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
t.addToBackStack(null);
t.commit();
}
mainActivity_layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
tools:ignore="MergeRootFrame">
<ListView
android:id="@+id/list"
android:layout_width="100px"
android:layout_weight="1"
android:layout_height="match_parent" />
<fragment
android:name="com.example.fragmentwithlistview.FragmentClass"
android:id="@+id/fragment00ID"
android:layout_toRightOf="@+id/list"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="4" />
FragmentClass_layout
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:ignore="MergeRootFrame"
android:background="#FFFFFF">
<TextView
android:id="@+id/tvID"
android:textColor="#000000"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
{{1}}
答案 0 :(得分:1)
最好使用静态方法进行实例化:
public class FragmentClass extends Fragment {
public static FragmentClass newInstance(Bundle b) {
FragmentClass fragment = new FragmentClass();
fragment.setArguments(b);
return fragment;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragmentclasslayout, null);
if (getArguments() != null) {
Log.i(TAG, "getArgument is not null");
int value = getArguments().getInt("pos");
TextView tv = (TextView) view.findViewById(R.id.tvID);
tv.setTextSize(value);
tv.setText("size = "+value*10);
} else {
Log.i(TAG, "getArgument is null");
}
return view;
}
}
然后简单地调用该方法并将arguments-bundle作为参数传递:
Bundle bundle = new Bundle();
bundle.putInt("pos", (position+1));
FragmentClass f = FragmentClass.newInstance(bundle);
此外,请勿使用onActivityCreated
获取FragmentClass中的参数,使用onCreateView
或必要onStart
或onResume
。看看片段生命周期:http://developer.android.com/guide/components/fragments.html#Lifecycle
答案 1 :(得分:0)
您可以在传递活动时使用
Bundle bundle = new Bundle();
bundle.putString("model", strDeckNumber);
final FragmentTransaction t = getActivity().getSupportFragmentManager().beginTransaction();
Frag1 newFragment = new Frag1();
newFragment.setArguments(bundle);
t.replace(R.id.fragment_container, newFragment);
t.addToBackStack(TAG);
t.commit();
接收片段
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
strSeriesNumber = getArguments().getString("model");
}