java - 为什么 BaseAdapter 的自定义子类只需要实现 Adapter 接口(interface)中的一些抽象方法?

标签 java android oop adapter baseadapter

我之前已经多次扩展了 BaseAdapter 和 ArrayAdapter 类,但关于 BaseAdapter 一直有一个令人烦恼的问题:为什么缺少一些来自 BaseAdapter 的方法? android.widget.Adapter 接口(interface)如 getView (常识当然会说你需要这个)会导致编译器错误,而其他接口(interface),如 hasStableIds 不会导致任何此类错误?我知道BaseAdapter继承了ListAdapter的这些方法,而ListAdapter又继承了Adapter的这些方法,但是为什么只需要实现几个,而其他的则不需要呢? android 文档没有在 BaseAdapter 文档中专门包含这些调用,因此如果只有一些需要实现,那么哪里有任何指示需要对 BaseAdapter 扩展进行这些特定调用?我知道我遗漏了一些东西,无论是对 oop 原理的理解还是阅读文档的能力。感谢您的见解!

注意:编译器错误为:

MyAdapter is not abstract and does not override abstract method getItemId(int) in Adapter

我找到的必需方法是:getItem、getCount、getItemId 和 getView

第二点:这些都是Adapter、ListAdapter中的抽象方法。

最佳答案

why does the absence of some methods from the android.widget.Adapter interface such as getView (common sense of course would say that you need this) cause compiler errors

因为BaseAdapter是一个抽象类,而getView()是一个抽象方法。

while others, like hasStableIds do not cause any such errors?

因为 BaseAdapter 有一个 hasStableIds() 的实现,子类可以根据需要使用它。

The android documentation does not include those calls specifically in BaseAdapter documentation

您可以在 BaseAdapter 文档的 Adapter 部分的继承方法中找到它们。

so where is any indication for the need for those specific calls for an extension of BaseAdapter if only some need implementation?

需要在 AdapterListAdapter 上定义但 BaseAdapter 未实现的方法。

BaseAdapter 实现的 AdapterListAdapter 上定义的方法不是必需的,但仍然可以如果需要的话可以被覆盖。

关于java - 为什么 BaseAdapter 的自定义子类只需要实现 Adapter 接口(interface)中的一些抽象方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18090232/

相关文章:

java - 使用 Android 的 Mobile Vision API 扫描二维码

java - 如何包装 Java String.format()?

android - 使用 android :layout_weight programmatically 填充 TableLayout

.net - 使用 DataContractJsonSerializer 将 Dictionary<TKey, TValue> 序列化为 JSON

c# - 为什么我不能在条件运算符中返回具有共同祖先的兄弟类型?

ruby - 这两段代码有什么区别?

java - 放入 Map<String, ?>

java - 从接口(interface)中的默认方法中访问 super 方法

android - SmsManager 成功

android - ViewPager - 从 setOnTabSelectedListener 启动 Activity