6 つのオプションを持つ FragmentList があり、クリックすると別の List Fragment に移動するとします。新しいフラグメントの6つのリストで選択されたアイテムを取得するにはどうすればよいですか? このクラス (ItemRolesList) の下にあるクラス (Item1List クラス) などの別のクラスで選択されたアイテムを取得できるようにするには、このクラス (ItemRolesList) に対して何をする必要がありますか。コードを投稿してください。このリンクは、何が起こっているのか理解できないため、アプリがクラッシュする原因を理解していないため、あまり役に立ちません。
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();
}
}
オプションの 1 つをクリックした場合のクラスの 1 つを次に示します。前の 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));
}
}