Access fragment from adapter

后端 未结 2 987
天涯浪人
天涯浪人 2021-01-18 17:10

The idea is i have a listview where each item is a product, when I click in the item, I need to go to another Fragment from the click which is inside the adapter of the list

相关标签:
2条回答
  • 2021-01-18 17:45

    You would need to pass it in your Constructor. For Example:

    public class ProductOffersListAdapter extends BaseAdapter
    {
     private Context context;
     private ArrayList<info.android.model.ProductsOffers> navProOffers;
     Fragment myFragment;
    
     public ProductOffersListAdapter(Context context, ArrayList<info.android.model.ProductsOffers> navProOffers, Fragment myFragment) 
     {
        this.context = context;
        this.myFragment = myFragment;
        this.navProOffers = navProOffers;
     }
    
    ...
    
    0 讨论(0)
  • 2021-01-18 17:45

    What I did to resolve this is:

    Intent i = new Intent(v.getContext(), MainActivity.class);                      
    i.putExtra("fragment_value", "2");
    v.getContext().startActivity(i);
    

    In main class I get the parameter as value and with a switch I set fragment in main which I am interested.

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