General Java option not possible?

I have a simple method that accepts a general List parameter, but for some reason my IDE (Eclipse) states how it cannot be resolved?

I'm doing something wrong here

private OnClickListener removeFieldListener(final LinearLayout layout, List<T> viewList) { return new OnClickListener() { @Override public void onClick(View v) { int indexToDelete = layout.indexOfChild(v); } }; } 
+4
source share
2 answers

In this case, the parameter T must be defined somewhere. As I think your class does not declare this parameter, you should put it in a method declaration, for example

 private <T> OnClickListener removeFieldListener(final LinearLayout layout, List<T> viewList) { 

But this will only move the problem to the caller of this method ...

+11
source

Riduidel is right that the problem is that you have not yet announced T

Depending on what you want to do with the contents of the list, most likely you can just use a wildcard. List<?> viewList will work if you get an Object out of it; or List<? extends IListener> List<? extends IListener> allows you to pull out IListeners, etc.

In general, you don't need a generic parameter if it appears only once inside your method, and you should use a wildcard instead. If it appears several times, for example, you delete things from the list and assign them to variables of type T , then you really need a template, and you have to parameterize your method as Rididel suggests .

+9
source

All Articles