Android How does the notifyDataSetChanged () method and ListViews work?

I am trying to understand the concept of ListView and how it works, and I am trying to create my own adapter that extends the BaseAdapter . For example, for ArrayAdapter there is a notifyDataSetChanged() method that should be called after updating the list of arrays, which contains all your data in order to update the ListView .

But I am creating my own subclass of BaseAdapter . This method is not available to me or is it? How to implement this method? In principle, what this method does, perhaps I will understand then.

In the case of the ArrayAdapter I assume that it looks at what position the ListView displayed, and checks to see if it matches the same name as the ArrayList after updating it? Or...

It says the method:

Informs interested observers that the baseline data has been changed and any view reflecting the data set should be updated.

But how exactly is it updated?

Can someone please explain?

+56
android android-listview android-adapterview
Sep 01 '12 at 17:21
source share
4 answers

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

+182
Sep 02
source share

BaseAdapter can be "observed" by other classes. When you call the ListView method setAdapter() , it calls the adapter registerDataSetObserver . So, the adapter knows who is interested in the new data.

Here you can check the source of the BaseAdapter . This is pretty small.

notifyDataSetChanged is available to you and you should not redefine it (because it does nothing special, so you can just reuse it in your class).

+13
Sep 01 '12 at 17:52
source share

Suppose your ListView displays some data stored in an ArrayList .

After changing the contents of the ArrayList you need to tell the list that the data source has been changed, and it needs to redraw it to show the new data.

So this is where notifyDatasetChanged() . It tells ListView that the data has been changed; and to show new data, ListView needs to be redrawn.

+9
Sep 01 '12 at 17:33
source share

As you extend the BaseAdapter for your own subclass creation, you will also get the notifyDataSetChanged() method.

0
Dec 08 '16 at 9:28
source share



All Articles