Run FragmentTransaction from inside ArrayAdapter

I have ListViewwith a few lines. Each row has button.

I want the start button of the FragmentTransaction to replace the fragment in which the ListView is located.

However, in the getView()method Adapterthis line does not work:

FragmentTransaction t = getContext().getSupportFragmentManager().beginTransaction();

This does not like the context.

Can this be done in this way or should the transaction occur elsewhere?

+5
source share
3 answers

getSupportFragmentManager()defined only for the class FragmentActivity, not for Context. Therefore, the compiler cannot resolve the call if you try to call it on an object of type Context.

, . , FragmentActivity Context. . FragmentActivity - , FragmentActivity , Context.

, :

if (getContext() instanceof FragmentActivity) {
    // We can get the fragment manager
    FragmentActivity activity = (FragmentActivity(getContext()));
    FragmentTransaction t = activity.getSupportFragmentManager().beginTransaction();
}
+10

, ,

FragmentTransaction ft = ((FragmentActivity)context).getSupportFragmentManager().beginTransaction();
+29

FragmentManager Adapter :

public class YourAdapter extends...

    private FragmentManage mFragmentManager;        

    public YourAdapter(FragmentManager fm) {
        mFragmentManager = fm;
    }

:

FragmentTransaction ft = mFragmentManager.beginTransaction();

Adapter Fragment.getFragmentManager() FragmentActivity.getSupportFragmentManager(),

+4

All Articles