Dima
Dima

Reputation: 1510

How to get 2 fragments of the same class with different content?

I have class:


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

Depending on the variable FLAG_BOOLEAN, i get different data from DB. So can i send this FLAG from Activity to fragments?

Upvotes: 0

Views: 116

Answers (2)

kayton
kayton

Reputation: 3759

If you're adding the Fragments programmatically, you can add a constructor for ConferenceListFragment that takes in your boolean flag and add fragments using FragmentTransaction, like so:

ConferenceListFragment list1 = new ConferenceListFragment(true);
ConferenceListFragment list2 = new ConferenceListFragment(false);

FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(parentView, list1);
ft.add(parentView, list2);
ft.commit();

With the constructor setting a private boolean member variable in ConferenceListFragment:

public ConferenceListFragment(boolean flag){
    this.flag = flag;
}

Then when you call your method, you can do:

List list = dbAdapter.getListItemInfoArray(this.flag);

Upvotes: 0

Robert Estivill
Robert Estivill

Reputation: 12477

You should use setArguments in the activity to pass them to the fragments and getArguments from within the fragments to retrieve them.

Upvotes: 1

Related Questions