13

i have a simple method that takes a generic List parameter but for some reason my IDE(Eclipse) states how it cannot be resolved?

Am i 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);

            }
        };
    }
1
  • I'll just add to Riduidel's answer, that it seems the generic parameter is not needed in this case, unless the OnClickListener is intended to use T as well. I.e., private <T> OnClickListener<T> removeFieldListener ... Commented Nov 12, 2010 at 10:53

2 Answers 2

21

In that case, the T parameter has to be defined somewhere. As I guess your class does not declares this parameter, you have to put it in your method declaration, like

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

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

Sign up to request clarification or add additional context in comments.

5 Comments

Actually, this shouldn't move the problem to callers - Java will use type inference to work out what the T is based on the type of the list being passed in. So there shouldn't be any additional burden on callers, compared to using a wildcard or the raw type.
ahh ok, how would i define this generic T in the class? i know i could do something like public class hello<T> but if i do that, the caller of this whole class would need to specify this generic type which i dont want to do as the method above is a private method and deals with private variables that are dealt from within this class not the caller of this class. im guessing i can do something along the declaration of this class with something like private Generic T; ?
@jonney - see in Riduidel's example, the <T> that's the second part of the signature. This means that the method is parameterised without you needing to add a generic parameter to the class. See the Generic Methods section of the generics FAQ.
Yup i got it. thanks. so in a nutshell, generics can be defined in the class definition or in a method definition. sweet. you learn something new every day. cheers to all
Can I identify the T belongs to which class instance inside the method ?
15

Riduidel is right in that the problem is that you haven't declared the T anywhere.

Depending on what you want to do with the contents of the list, chances are you can just use a wildcard. List<?> viewList would work if you're only pulling Objects out of it; or List<? extends IListener> will allow you to get IListeners out of it, etc.

In general, you don't need a generic parameter if it only appears once within your method, and you should use a wildcard instead. If it does appear multiple times, for example you remove things from the list and assign them to variables of type T, then you do indeed need the wildcard and you should parameterise your method as Riduidel suggests.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.