-1

I was following a tutorial on creating a custom ArrayAdapter for a ListView in Android. Here is the custom adapter class from the tutorial:

public class NumbersViewAdapter extends ArrayAdapter<NumbersView> { 
  
    // invoke the suitable constructor of the ArrayAdapter class 
    public NumbersViewAdapter(@NonNull Context context, ArrayList<NumbersView> arrayList) { 
        
          // pass the context and arrayList for the super  
          // constructor of the ArrayAdapter class 
        super(context, 0, arrayList); 
    } 

How does the argument pass in doesn't have to match the constructor in the super class? To be more clear, I thought the argument pass in: NumbersViewAdapter(@NonNull Context context, ArrayList<NumbersView> arrayList) has to match with: super(context, 0, arrayList)

second of all, I saw no constructor has the argument pass in as such (@NonNull Context context, ArrayList<NumbersView> arrayList). The only thing I think is close with it is

public ContactListAdapter(@NonNull Context context, int resource, @NonNull ContactInfo[] objects) {
        super(context, resource, objects);
    }

I haven't try out anything yet

1
  • 1
    Why do you think that a subclass constructor should match its supperclass constructor? All a constructor has to do is call any constructor of its superclass (either explicitly, or otherwise an implicit call to super() is injected by the compiler), either with values it received from its caller, values it derived, or some fixed values. There is no requirement for a constructor of a subclass to be identical to its superclass (otherwise all classes would only have a no-arg constructor, because all classes are ultimately a subclass of Object. Commented Jul 4 at 8:08

1 Answer 1

1

Basically, this is a constructor overloading. This concept is used to provide various version of your class initialised with different parameters. By doing so, you can accept optional parameter or sometimes not to pass at all with minimum invocations.

Check this reference to understand it in details.


Back to your question:

The constructor that you have

NumbersViewAdapter(@NonNull Context context, ArrayList<NumbersView> arrayList)

does not exists on the ArrayAdapter.

Check the public constructors available on ArrayAdapter here:

Since, the closest contractors to your implementation is following:

public ArrayAdapter (Context context, int resource, List<T> objects)

You have to pass one more parameter for resource in the super call in the middle.

Hence, you have to call super(context, 0, arrayList); with resource as 0.

Although you can actuality provide any resource as layout file as well which can be used for this adapter item view later on.

1
  • 1
    This is not constructor overloading. Constructor overloading is if a class itself has multiple constructors. In this case the subclass simply has its own constructor. There is no requirement that a subclass constructor needs to match the superclass constuctor. Commented Jul 4 at 8:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.