android - There is no default constructor for ArrayAdapter

ERJAN

I m making adapter to adapt my book collection to be visible in list view.

kk

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 ;
}

However, I want to know why I need this argument (int position) and call superclass constructor?

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

Also, in the broader sense why do we always(?) need to call super.onCreate like here in every onCreate?

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?

CommonsWare

Issue is solved if I add super(context, position):

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.

However, I want to know why I need this argument (int position) and call superclass constructor?

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.

Also, in the broader sense why do we always(?) need to call super.onCreate like here in every onCreate?

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.

yet we still have to call super's in each of them?

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Android Spinner issue - The constructor ArrayAdapter<String>(NewSRO, int, String[]) is undefined

Android AsyncTask with ArrayAdapter gives error "Cannot resolve constructor"

Cannot resolve constructor ArrayAdapter

Cant resolve constructor ArrayAdapter

constructor of class ArrayAdapter

"This class should have a default constructor" [Android]

BroadcastReceiver has no default constructor in android manifest

What is the second argument in ArrayAdapter() constructor?

"Cannot resolve constructor ArrayAdapter()" error

Cannot resolve constructor ArrayAdapter(android.Content.Context, int, java.util.ArrayList<MyObject>

Is constructor generated default constructor?

ArrayAdapter in Fragment - Android

Extending ArrayAdapter in android

Android ListView and ArrayAdapter/Arraylist

Spinner and ArrayAdapter NullPointerException in Android

ArrayAdapter use Kotlin android

ArrayAdapter initialization error (Android)

CheckBox and TextView in ArrayAdapter in Android

Android ArrayAdapter NullPointerException getID

Android textview swipe with ArrayAdapter

android UnsupportedOperationException in ArrayAdapter<String>

Custom ArrayAdapter for a ListView (Android)

ListView problem "cannot resolve constructor arrayadapter anonymous"

What resource is the id parameter in ArrayAdapter constructor?

Cannot resolve constructor when using ArrayAdapter

Cannot resolve constructor ArrayAdapter in custom view

Android: Signed APK issue - no default constructor when there is one

Xamarin Android app crashes in release mode (default constructor not found)

using default constructor in constructor inheritance