Java Parameter Generic interface

The name of the picture


Java Parameter Generic interface



i have a problem with parameter generic interface. I have 2 different interface and i need to pass the interface to constructor but the constructor gives warning.


public interface FormFragmentContract {
interface View extends BaseView {
void updateWorkshopUI(int icon);
void showRemoveFavoriteDialog(String wsId, String name);
void favoriteOnClick();
void showSnackbar();
}
}

public interface WorkshopListContract {
interface View extends BaseView {
void initializeMap();
void onSuccessInitMap();

void favoriteOnClick();
void showSnackBar(String message);
}
}



and the constructor code this gives warning like below


workshopAdapter = new WorkshopAdapter(mActivity, workshopList, this); <- this give warning mismatch type

public WorkshopAdapter(Context context, List<Workshop> workshops, //NEED TO BE GENERIC) {
this.context = context;
this.workshops = workshops;
}



How to make both of my interface generic so it can take both interfaces as parameter ?



EDIT : because both interface contain favoriteOnClick() , If i pass the FormFragmentContract into constructor i can use the favoriteOnClick() from FormFragment and vice versa with WorkshopListContract if i pass that to the constructor as well





So do I understand you correctly, you either want FormFragmentContract OR WorkshopListContract to be able to put into the constructor?
– Lino
13 mins ago




FormFragmentContract


WorkshopListContract





@Lino see edited question
– Yonathan Hans
6 mins ago




1 Answer
1



You'd have to declare an upper interface which contains the method favoriteOnClick():


interface


favoriteOnClick()


public interface Contract { // name it as you wish
void favoriteOnClick();
}



And then extend both your interfaces from said created:


interfaces


public interface FormFragmentContract extends Contract{
// methods
}

public interface WorkshopListContract extends Contract{
// methods
}



And then accept the Contract interface in your constructor:


Contract


public WorkShopAdapter(Context context, List<Workshop> workshops, Contract contract) {
this.context = context;
this.workshops = workshops;

// call it here
contract.favoriteOnClick();
}



No need for generics






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived