假设我有一个包含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));
}
}
答案 0 :(得分:1)
您必须使用该活动将数据从一个片段传递到另一个片段。您可以在一个片段中使用getActivity()来获取活动,然后到达另一个片段。
您可以查看以下答案: