假设我有一个带有 6 个选项的 FragmentList,单击它会将我带到另一个 List Fragment。我如何才能在新片段的 6 列表中选择项目?我必须对这个类(ItemRolesList)做些什么才能让我能够在另一个类中选择项目,例如这个类(Item1List类)下面的一个。请张贴代码。此链接不是很有帮助,因为当我尝试执行它所说的操作时,我不明白发生了什么导致我的应用程序崩溃:http: //developer.android.com/guide/components/fragments.html#CommunicatingWithActivity
public class ItemRolesList extends ListFragment{
ArrayList<String> itemRoles;
TestAdapter mDbHelper;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
mDbHelper = new TestAdapter(getActivity());
mDbHelper.createDatabase();
mDbHelper.open();
//////Get what we need from the database/////////
itemRoles = mDbHelper.getTableColumn("role", new String[] {"name"});
/////////////////////////////////////////////////
//Now close the database.
mDbHelper.close();
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, itemRoles));
///////////////////////////////////////////////////////////
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String libraryList;
//Get the position the user clicked.
Fragment newFragment = null;
libraryList = l.getItemAtPosition(position).toString();
if(libraryList.equals("Item1")){
newFragment = new Item1List();
}else if (libraryList.equals("Item2")){
newFragment = new Item2List();
}else if (libraryList.equals("Item3")){
newFragment = new Item3List();
}else if (libraryList.equals("Item4")){
newFragment = new Item4List();
}else if (libraryList.equals("Item5")){
newFragment = new Item5List();
}else if (libraryList.equals("All Items")){
newFragment = new AllItemsList();
}
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.tab2, newFragment);
transaction.addToBackStack(null);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.commit();
}
}
如果他们要单击其中一个选项,这是其中一个类。我必须在这个类中做什么才能获得在上一个 ListFragment 类中选择的项目?
public class Item1List extends ListFragment{
ArrayList<String> specificItemType;
TestAdapter mDbHelper;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
mDbHelper = new TestAdapter(getActivity());
mDbHelper.createDatabase();
mDbHelper.open();
specificItemType = mDbHelper.getMultiTableData("item.[name]", "item", "item_role", "item.[id]", "item_role.[item_id]", "role_id = 4");
//Close the database.
mDbHelper.close();
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, specificItemType));
}
}