Some editable views will be used during Android development, or some functions that need to be automatically updated as data changes. Most of these views require data synchronization to update the display of views. In addition, the data type is complex, so it is not suitable to add data update Methods to each class to process the display of views. A simpler and more convenient way is to use a data listener. This is a bit like the data listening method of property change in JavaBean. When the data changes, the listener is registered for the data, and the view can be updated in real time. Below we will write out the code for this usage and make some changes.
The data structure used is hashmap.
Public class hearingmap <K, V> extends hashmap <K, V >{< br/> Private Static final long serialversionuid = 1l; </P> <p> private listener <K, v> listener; </P> <p> // interface for the data element change listener <br/> Public static interface listener <K, V >{< br/> void onadd (K key, V value); <br/> void onremove (K key ); <br/>}</P> <p> // shocould be override For subclass <br/> @ override public v put (K key, V value) {<br /> V ret = super. Put (Key, value); <br/> If (listener! = NULL) {<br/> If (Ret! = NULL) {<br/> listener. onremove (key); <br/>}< br/> listener. onadd (Key, value); <br/>}< br/> return ret; <br/>}< br/> // shocould be override For subclass <br/> @ suppresswarnings ("unchecked") <br/> @ override public v remove (Object key) {<br/> V ret = super. remove (key); <br/> If (listener! = NULL) {<br/> If (Ret! = NULL) {<br/> listener. onremove (k) Key); <br/>}< br/> return ret; <br/>}< br/> // setter for the listener <br/> Public void setlistener (listener <K, V> listener) {<br/> This. listener = listener; <br/>}< br/>}
Implement the data structure in activity, use internal private classes, and notify simpleadapter.
Public class sampleactivity extends managedactivity {<br/> private list <Map <string, string> datas = new arraylist <Map <string, string> (); <br/> private simpleadapter dataadapter; <br/> private spinner dataviewlist; <br/>... </P> <p> private hearingmap. listener <string, databean> datalistener = new hearingmap. listener <string, databean> () {</P> <p> // implementation <br/> Public void onadd (string key, databean value) {<br/> Map <string, string> datamapping = new treemap <string, string> (); <br/> datamapping. put ("name", value. getname (); <br/> datamapping. put ("ID", value. GETID (); <br/> sampleactivity. this. datas. add (datamapping); <br/> sampleactivity. this. dataadapter. notifydatasetchanged (); <br/>}</P> <p> // implementation <br/> Public void onremove (string key) {<br/> iterator <Map <string, string> itr = datas. iterator (); <br/> while (itr. hasnext () {<br/> If (itr. next (). get ("ID "). equals (key) {<br/> itr. remove (); <br/> // policy <br/> sampleactivity. this. dataadapter. notifydatasetchanged (); <br/> return; <br/>}< br/> }; </P> <p> protected void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); </P> <p>... </P> <p> setcontentview (R. layout. spinner_layout); <br/> dataviewlist = (spinner) findviewbyid (R. id. splist); </P> <p> dataadapter = new simpleadapter (this, datas, <br/> r. layout. spinner_item, new string [] {"name", "ID"}, new int [] {R. id. name, R. id. id}); <br/> dataviewlist. setadapter (dataadapter); </P> <p>... <br/>}</P> <p>}
Register listener
Private void attachdatalistener () {<br/> DataFactory. getinstance (). setlistener (datalistener); <br/>}
In this way, you can implement listener for different view classes to process your own data.