0

Say I have a FragmentList with 6 options and when clicked it takes me to another List Fragment. How would I be able to get the item selected in that list of 6 in the new Fragment? What would I have to do to this Class (ItemRolesList) for me to be able to get the item selected in another class such as the one below this (Item1List Class). Please post code. This link is not very helpful as I do not understand what is going on cause my app crashes when I try to do what it says: 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();
}
    }

Here is one of the classes if they were to click one of the optiones. What would I have to do in this class for me to be able to get the item selected in the previous ListFragment class?

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));

}
}
JoeyL
  • 1,295
  • 7
  • 28
  • 50

1 Answers1

1

You have to use the activity to pass the data from one fragment to another. You can use getActivity() in one fragment to get the activity and then reach the other fragment.

You can check on this answer:

Passing data between a fragment and its container activity

Community
  • 1
  • 1
lalborno
  • 414
  • 3
  • 5
  • Did I not supply enough information for someone to post a small portion of code to help me out? – JoeyL Sep 05 '12 at 18:40