android-没有ArrayAdapter的默认构造函数 [英] android - There is no default constructor for ArrayAdapter
问题描述
我正在制作适配器,以使我的藏书在列表视图中可见.
I m making adapter to adapt my book collection to be visible in list view.
如果我添加super(context,position),问题就解决了:
Issue is solved if I add super(context, position):
public BookAdapter(Context context, int position, List <Book> updatedBooksList) {
super(context, position);
this.context = context;
this.booksList = updatedBooksList ;
}
但是,我想知道为什么我需要这个参数(int位置)并调用超类构造函数?
However, I want to know why I need this argument (int position) and call superclass constructor?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
而且,从广义上讲,为什么我们总是(?)在每个onCreate中都需要像这里一样调用super.onCreate?
Also, in the broader sense why do we always(?) need to call super.onCreate like here in every onCreate?
我们不应该覆盖所有活动生命周期阶段- onPause,onREsume,onStop,OnDestroy,但我们仍然有在其中的每一个中都调用super吗?
Aren't we supposed to override all our activity lifecycle stages - onPause, onREsume, onStop, OnDestroy, yet we still have to call super's in each of them?
推荐答案
如果我添加super(context,position),问题就解决了:
Issue is solved if I add super(context, position):
第二个参数不是position
.它是布局资源的ID,默认情况下用于ArrayAdapter
创建的行所使用的ID.您可以通过阅读您正在调用的构造函数的JavaDocs .
The second parameter is not a position
. It is the ID of a layout resource, one that is used by default for the rows created by the ArrayAdapter
. You can tell that by reading the JavaDocs for the constructor that you are calling.
但是,我想知道为什么我需要这个参数(int位置)并调用超类构造函数?
However, I want to know why I need this argument (int position) and call superclass constructor?
因为,正如IDE告诉您的那样,ArrayAdapter
上没有默认的构造函数.在Java中,默认构造函数"是零参数构造函数. Java中的每个构造函数都需要链接到超类构造函数.如果超类具有零参数(默认")构造函数,则Java将自动链接到它.如果超类没有零参数构造函数,则需要手动链接到超类构造函数.
Because, as the IDE told you, there is no default constructor on ArrayAdapter
. In Java, the "default constructor" is a zero-argument constructor. Every constructor in Java needs to chain to a superclass constructor. If the superclass has a zero-argument ("default") constructor, Java will chain to it automatically. If the superclass does not have a zero-argument constructor, you need to chain to a superclass constructor manually.
而且,从广义上讲,为什么我们总是(?)在每个onCreate中都需要像这里一样调用super.onCreate?
Also, in the broader sense why do we always(?) need to call super.onCreate like here in every onCreate?
因为创建Activity
的开发人员选择将其作为一项要求.
Because the developers who created Activity
elected to make that a requirement.
我们不应该覆盖所有活动生命周期阶段
Aren't we supposed to override all our activity lifecycle stage
不.您将覆盖所需的那些内容.
No. You override those that you need.
但是我们仍然必须在它们每个中都调用super吗?
yet we still have to call super's in each of them?
Activity
除了要执行的任何操作外,还希望对那些生命周期方法进行处理.为了强制执行此操作,Activity
要求您链接到超类,如果没有,则抛出SuperNotCalledException
.
Activity
wants to do its processing for those lifecycle methods, in addition to anything that you want to do. To enforce that, Activity
demands that you chain to the superclass and throws a SuperNotCalledException
if you do not.
这篇关于android-没有ArrayAdapter的默认构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!