How to call methods in fragment from adapter Android… here is a solution to the problem.
How to call methods in fragment from adapter Android
I
need help, so I have a fragment that has a RecycleView and a button inside the RecycleView
.
The clicked button must open the dialog already declared in the base fragment, so I just call openDialog(DIALOG_CHECK);
Now how do I call that dialog on my adapter, I’ve created a method in fragment and call it from the adapter and produce the error “Java lang null pointer”
Here is my code :
DeliveryFragment delivFrag = new DeliveryFragment();
holder.editButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
delivFrag.doEdit();
}
});
in fragment
public void doEdit(){
openDialog(DIALOG_EDIT_ITEM);
}
Solution
Update your adapter constructor to accept 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 adapter:
((FragmentName)fragment).methodName();