I got it . I could not understand how the hell the adapter started and how he found out where to get the data. When I expanded the BaseAdapter class, in the constructor of this class I initialized the list of elements that I wanted to see in the ListView . But I could not understand how these values would be used and when.
So here is the thing !!! :
There are several methods in BaseAdapter that need to be overridden. Among them is getCount() .
When the ListView created and much more, it calls getCount() . If this returns a value other than 0 (I returned the size of the ArrayList that I previously initialized in the constructor), then it calls getView() enough times to fill the screen with elements. For example , I initialized an ArrayList with 20 elements. Since only 8 elements were initially placed on the screen, getView() was called 8 times, each time asking for the position that I had to return (more precisely, he wanted to know what the line in the list would look like at that particular position). what data needed to be contained). If I scroll down the list, getView() again and again until I reach the end of the list, in my case 20 items / rows.
What notifyDataSetChanged() does ... when it is called, it looks at what elements are displayed on the screen at the time it is called (more precisely, which row indices), and calls getView() with these positions.
those. if you display the first 8 elements in the list (that is, those that are visible on the screen) and you add another element between the 2nd and 3rd elements in the list and call notifyDataSetChanged() then getView() 8 times, with positions, starting with 0 and ending with 7, and since you get data from ArrayList in getView() it will automatically return the new element inserted into the list, along with 7 from the previous 8 (7). not 8, because the last item has moved down one position, so that it is no longer visible), and the ListView will redraw, or something else, with these items.
In addition, it is important to indicate that if you implemented getView() correctly, you will end up reworking already displayed elements (objects) (instead of creating new ones). Watch this video at about 12:00 minutes to see the correct way to implement getView()
I realized all this by placing LogCat calls in each method and tracking what was happening.
Hope this helps someone who is just starting to understand how ListView works.
PS This example also helped me a lot.
UPDATE
ListViews no longer in use. Android has released RecyclerView which does the processing of views for you, but knowing the basics of ListView helps you understand RecyclerView .
Here's a link for reference: https://developer.android.com/guide/topics/ui/layout/recyclerview