getFragmentManager from ArrayAdapter

前端 未结 2 1865
我寻月下人不归
我寻月下人不归 2021-01-12 13:14

I have a listView fulled from my custom ArrayAdapter. In each view there is a button. I want to change the current fragment when the button is clicked. This is my code:

相关标签:
2条回答
  • 2021-01-12 13:49

    its so late but here no one answered this question so.

    you can invoke fragment manager in your getview method using

    FragmentManager fm = ((Activity)context).getFragmentManager(); 
    

    getFragmentManager() is a method of Activity class .

    for example

    holder.tvBuy.setOnClickListener(new OnClickListener()
    {
    
    @Override
    public void onClick(View v) 
    {
        // TODO Auto-generated method stub
         Fragment fragment;
         fragment = new Buy();
        ((Activity)context).getFragmentManager().beginTransaction().replace(R.id.content_frame,fragment).commit();
    }
    }
    

    here you need to pass your activity as context to your adapter.

    hope it will help someone....

    0 讨论(0)
  • 2021-01-12 14:05

    OK, maybe i'm a bit late but, maybe it will help some people to do this.

    In the class where you call the adapter, you need to extends or implements "FragmentActivity". Then your Activity will contain a fragmentManager.

    When you call your Adapter call it with the class that extends FragmentActivity

    MyAdapterName = new MyAdapterName(MyClassName.this, Objects);
    

    Then in your adapter in getView() - do this.

    final Context context = parent.getContext();
    FragmentManager fm = ((Activity) context).getFragmentManager();
    

    Be carefull to use the good android FragementManager - android.app.FragmentManager not android.support.v4.app.FragmentManager;

    0 讨论(0)
提交回复
热议问题