Android 如何获得具有不同内容的同一类的2个片段?
我有课:Android 如何获得具有不同内容的同一类的2个片段?,android,android-fragments,android-listfragment,Android,Android Fragments,Android Listfragment,我有课: public class ConferenceListFragment extends SherlockListFragment { @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); DBAdapter dbAdapter = new DBAdapter(n
public class ConferenceListFragment extends SherlockListFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
DBAdapter dbAdapter = new DBAdapter(new DBHelper(getSherlockActivity()));
//How to get veriable "FLAG_BOOLEAN" ?
List list = dbAdapter.getListItemInfoArray(FLAG_BOOLEAN);
dbAdapter.close();
CustomListAdapter adapter = new CustomListAdapter(getActivity(), list);
setListAdapter(adapter);
}
}
根据变量标志_BOOLEAN,我从DB中获得不同的数据。
那么我可以将此标志从“活动”发送到片段吗?您应该在“活动”中使用此标志将其传递到片段,并从片段中检索它们。如果您以编程方式添加片段,您可以为ConferenceListFragment添加一个构造函数,该构造函数接受您的布尔标志,并使用FragmentTransaction添加片段,如下所示:
ConferenceListFragment list1 = new ConferenceListFragment(true);
ConferenceListFragment list2 = new ConferenceListFragment(false);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(parentView, list1);
ft.add(parentView, list2);
ft.commit();
构造函数在ConferenceListFragment中设置私有布尔成员变量时:
public ConferenceListFragment(boolean flag){
this.flag = flag;
}
然后,当调用方法时,可以执行以下操作:
List list = dbAdapter.getListItemInfoArray(this.flag);
我试过了。Logcat告诉我构造函数必须为空。所以有一个问题,啊。Robert Estivill的方法可能是首选的方法。