[英]Overriding setAdapter in subclass of ListView
I'm trying to override method setAdapter in a subclass of ListView. 我试图在ListView的子类中重写setAdapter方法。 setAdapter is defined as setAdapter定义为
void setAdapter(T adapter);
in AdapterView class. 在AdapterView类中。 AdapterView class is generic class AdapterView AdapterView类是通用类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. 上面的声明不适用于ListView子类,因为类型变量不可用,但是我不明白如何将类型变量引入子类。 Any concrete type doesn't work. 任何具体类型均无效。
It's true that AdapterView
uses a generic type argument, but ListView
and GridView
specify that the type must be a ListAdapter
. 的确, AdapterView
使用泛型类型参数,但是ListView
和GridView
指定类型必须为ListAdapter
。 So you can do this: 因此,您可以执行以下操作:
@Override
public void setAdapter(ListAdapter adapter) {
// ...
}
This is because ListView
's parent class, AbsListView
, extends AdapterView<ListAdapter>
: 这是因为ListView
的父类AbsListView
扩展了AdapterView<ListAdapter>
:
public abstract class AbsListView extends AdapterView<ListAdapter> { ... }
Which means T
is a ListAdapter
everywhere it's used in ListView
. 这意味着T
在ListView
使用的ListAdapter
地方都是ListAdapter
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.