Victor Denisov
Victor Denisov

Reputation: 747

Overriding setAdapter in subclass of ListView

I'm trying to override method setAdapter in a subclass of ListView. setAdapter is defined as

void setAdapter(T adapter);

in AdapterView class. AdapterView class is generic class AdapterView

The declaration from above doesn't work for ListView subclass because type variable is not available, but I don't understand how to introduce the type variable into subclass. Any concrete type doesn't work.

Upvotes: 1

Views: 450

Answers (1)

cnnr
cnnr

Reputation: 1636

It's true that AdapterView uses a generic type argument, but ListView and GridView specify that the type must be a ListAdapter. So you can do this:

@Override
public void setAdapter(ListAdapter adapter) {
    // ...
}

This is because ListView's parent class, AbsListView, extends AdapterView<ListAdapter>:

public abstract class AbsListView extends AdapterView<ListAdapter> { ... }

Which means T is a ListAdapter everywhere it's used in ListView.

Upvotes: 1

Related Questions