1

I'm creating an application and I have a FragmentPager that I use. To use FragmentPager I must support it with a FragmentPagerAdapter. I have an interface that I've built the called Nameable and I want to create an array list that you can put there Fragments that implements the Nameable interface.

I've tried those ways but it gives me an error, and I don't really know why because it's a java thing and there are a lot of examples like this over the internet.

This is what I've tried:

private ArrayList<Fragment extends Nameable> mFragmentList;

The error:

Syntax error on token "extends", , expected

Other thing (which is not so right way to do it but I tried it to)

private ArrayList<? extends Fragment & Nameable> mFragmentList;

The error:

Syntax error on token "&", , expected

Why does this statement give me an error? I'm pretty sure that's the right way to do it.

Thanks, Elad.

1 Answer 1

3

This should be enough:

private ArrayList<Fragment> mFragmentList;

extends and super in generics signatures are used to constrain type parameters, not actual types. That Fragment implements Nameable is already defined in the Fragment class, you don't need to repeat that everywhere.

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

8 Comments

Of course, but I do want to constrain the type, because I need it. Example: @Override public String getTitle(int position) { return mFragmentList.get(position).getName(); }
Wait, you mean that Fragment doesn't implement Nameable? If so, make a class/interface that does, and make the list of its type.
No, Fragment doesn't implements Nameable. It's a bit complicated, Fragment has some subclasses, and I want to put in this list Fragments subclasses, and Fragment subclasses subclasses (let's say ListFragment extend Fragment and my class extends ListFragment) and they all must implement Nameable. Did you understand?
Then you need to make some sort of NameableFragment class or interface, and store that in the list.
But if I'll make NameableFragment that extends Fragment I won't be able to put objects of classes that extends ListFragment (a subclass of Fragment).
|

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.