Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call method in fragment from adapter Android

I need help so I have a fragment which has a RecycleView and inside the RecycleView there is a button.

The button after click must open the dialog which already declared in base fragment so I only call like openDialog(DIALOG_CHECK);

Now how can I call that dialog on my adapter I already make a method in fragment and call it from the adapter and make an error "Java lang null pointer"

This is my code :

DeliveryFragment delivFrag = new DeliveryFragment();
holder.editButton.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         delivFrag.doEdit();
     }
});

And in fragment

public void doEdit(){
   openDialog(DIALOG_EDIT_ITEM);
}
like image 926
Evan Laksana Avatar asked Apr 01 '16 03:04

Evan Laksana


1 Answers

Update your adapter constructor to accept the Fragment as a parameter.

customAdapter = new CustomAdapter(myContext, android.R.layout.simple_list_item_1, getList, HomeFragment.this);

Adapter Class:

public CustomAdapter(Context context, int id, HomeFragment fragment) {
    this.fragment = fragment;
}

Call in Adapter :

((FragmentName)fragment).methodName();
like image 195
assif_tiger Avatar answered Sep 21 '22 06:09

assif_tiger