Reputation: 7486
I want to return a callback from a custom adapter to my fragment. However the adapter won't let me pass the reference of the Fragment implementing the interface defined in custom adapter. How can I receive callbacks from adapter to fragment?
public class MyFrag extends Fragment implements MyInterface
{
@Override
public void onCreateView()
{
MyAdapter adapter = new MyAdapter(this); // error here: How to resolve
}
@Override
public void mySignal()
{
}
}
public class MyAdapter extends RecyclerViewAdapter
{
MyInterface listener;
public MyAdapter(MyInterface listener)
{
this.listener = listener;
}
public interface MyInterface
{
public void mySignal();
}
}
Upvotes: 1
Views: 1270
Reputation: 612
As I know, Fragment has this method
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
Sorry for my question! What is onCreateView() method without any params?
This way will work fine if there is not any different things more.
Upvotes: 0
Reputation: 3504
Just for a clear definition and SOC you could try this instead,
public class MyFrag extends Fragment
{
@Override
public void onCreateView()
{
MyAdapter adapter = new MyAdapter(new MyInterface() {
@Override
public void mySignal()
{
//do stuff here
}
});
}
}
Upvotes: 1