Search code examples
androidlistviewandroid-arrayadaptercustom-adapter

How does getcount() in array adapter return values without returing array size explicitly?


This question may sound stupid but I cannot help myself but ask here.

I know in Array adapter, getcount() method is used to return the number of elements to display in the list. I also know that we need to override it and return the size of the array or number of lists to display in the layout.

In many online blogs, I saw that the getcount() method is not overridden but still, the list is displayed and when I try to do it the same way, I do not get any lists displayed unless I override getcount() .

So my question is

1) Is it really necessary to override method to display lists? How does getcount method know how many lists to display without overriding it as it is done in many online blogs.

2) when it is not necessary to override getcount() and still get the views as intended.

I am just learning and hope you to understand. Thank you


Solution

    1. inside adapter (not list itself) - yes, you have to override this method and return number of items (e.g. straight from list.size())
    2. show these "many online blogs links", will verify. some adapters (like base ArrayAdapter) may have automated counting items, especially when they are passed in constructor in List - this class is well-known collection, so there is prepared adapter for this common usage, but if you have YourOwnClass populated by own resolution, then you have to extend adapter and handle counting by self
    3. show some code... always