the ListView needs to use the data adapter, adapter, to display the data. When we delete an entry in the ListView, the data source in the data adapter is bound to change, when we call the Notifydatasetchanged method provided by the adapter class to notify the ListView that the data has changed and request a redraw. This actually uses a more common design pattern, the Observer pattern.
Before analyzing the observer patterns involved in data adapters, let's take a quick look at what the Observer pattern is. the definition of the Observer pattern : Defines a one-to-many dependency between objects, so that whenever an object changes state, all objects that depend on it are notified and automatically updated.
Above is the class diagram of the Observer pattern. In the subject class, the Attach/detach method is used to bind/unbind one or more observers (Observer) objects, and when a subject occurs with an event that is of interest to Observer, Subject will call the Notify method to notify all the bound observer objects and call their Update method for updating the data.
Below we try to analyze the observer pattern in the data adapter. Here we can start tracking the source code from the Baseadapter notifydatasetchanged. Locating this method, we find that there is only one line of code that calls the Notifychanged method of the Mdatasetobservable object.
public void notifydatasetchanged () { mdatasetobservable.notifychanged (); }
This mdatasetobservable is the object being observed, and it is a datasetobservable type. Let's look at its implementation:
Package Android.database;public class Datasetobservable extends observable<datasetobserver> {public void Notifychanged () { synchronized (mobservers) {for (int i = Mobservers.size ()-1; I >= 0; i--) { mobservers. Get (i). onChanged ();}}}
As you can see, each observer's OnChanged callback method is called in its Notifychanged method, which is the Observer collection, which is defined in Datasetobservable's parent class observable, and in addition, Here, the generic type specifies that the Observer type must be of type Datasetobserver. We open observable Source:
Package Android.database;import Java.util.arraylist;public Abstract class Observable<t> {protected Final Arrayli st<t> mobservers = new arraylist<t> (); public void Registerobserver (T observer) {if (Observer = = null) {throw new IllegalArgumentException ("t He observer is null. "); Synchronized (mobservers) {if (Mobservers.contains (Observer)) {throw new IllegalStateException ( "Observer" + Observer + "is already registered."); MOBSERVERS.ADD (Observer); }} public void Unregisterobserver (T observer) {if (Observer = = null) {throw new illegalargument Exception ("The Observer is null."); } synchronized (mobservers) {int index = MOBSERVERS.INDEXOF (Observer); if (index = =-1) {throw new IllegalStateException ("Observer" + Observer + "is not registered."); } mobservers.remove (index); } } ...} This class defines a ArrayList type of observer collection, and provides two methods to register/unregister an observer, which is actually the Remove/add method of invoking the collection. Here we understand that the Notifydatasetchanged method that invokes the adapter will eventually notify all the registered observers, invoking the OnChanged method of each observer. If the ListView wants to update the interface after deleting an entry, it must have registered an observer before, and the Observer's OnChanged method will inevitably have the code for the interface redraw. The way the ListView deals with the adapter is the Setadapter method, and it is conceivable that there is a code for the registered observer in this method. According to this idea, we locate the Setadapter method of the ListView:
public void Setadapter (ListAdapter adapter) { ... Super.setadapter (adapter); if (madapter! = null) { ... Mdatasetobserver = new Adapterdatasetobserver (); Madapter.registerdatasetobserver (mdatasetobserver); ... ... } else { ... } Requestlayout (); }
sure enough, in this method we found the code of the registered observer, but the observer here is Adapterdatasetobserver type, and adapter requires the Datasetobserver type, then obviously, Adapterdatasetobserver is a subclass of Datasetobserver. The definition of this class is in the parent class Abslistview of the ListView:
Class Adapterdatasetobserver extends Adapterview<listadapter>. Adapterdatasetobserver { @Override public void onChanged () { super.onchanged (); if (Mfastscroller! = null) { mfastscroller.onsectionschanged ();} } ... ... }
Class Adapterdatasetobserver extends Datasetobserver { private parcelable minstancestate = null; @Override public void onChanged () { mdatachanged = true; Molditemcount = Mitemcount; Mitemcount = Getadapter (). GetCount (); if (AdapterView.this.getAdapter (). Hasstableids () && minstancestate! = null && molditemcount = 0 & amp;& mitemcount > 0) { AdapterView.this.onRestoreInstanceState (minstancestate); Minstancestate = null; } else { remembersyncstate (); } Checkfocus (); Requestlayout (); } ... ... }
As you can see, this class does inherit datasetobserver and calls Requestlayout in onchanged to refresh the layout. Here we understand the whole process, but alsoIt's really powerful to see how the Observer pattern is used in actual projects. Finally, attach a picture to make it easy for everyone to understand.
Observer mode in the "Android Notes" data adapter (adapter)