An important control in Android is the ListView, which displays data bindings in the ListView by enabling the user to implement the Baseadapter class to connect the ListView and data lists. Adapter mode connects two incompatible interfaces, enabling two of the two interfaces that do not work together to implement the functions that work together.
The example validates the adapter pattern by mimicking the Android ListView implementation process, which is structured as follows:
650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/8B/7E/wKiom1hPkoKi66GsAAAkjHx4RHQ417.png-wh_500x0-wm_3 -wmp_4-s_558318750.png "title=" adapter mode. png "alt=" wkiom1hpkoki66gsaaakjhx4rhq417.png-wh_50 "/>
Implementation code:
Var adapter = class.extend ({listview:null,datachanged:function () {if (this.listview != null) {Listview.draw ();}},getview:function (position) {return "";},getcount:function () {return 0;} , Getobject:function (index) {return null;}}); Var listview = class.extend ({id:null,adapter:null,ctor:function (_id) {this.id = _id;}, Setadapter:function (_adapter) {this.adapter = _adapter;_adapter.listview = this;}, Setonitemclick:function (_callback) {this.callback = string (_callback). Replace (/^function (\s|\n) + (. +) \ ((.| \ n) +$/, ' $ $ '),},draw:function () {var txt = "", if (this.adapter != null) {for (var i = 0;i < this.adapter.getcount (); i++) {txt += "<div onclick = ' + this.callback + ' (" + i + ") "+ " ' > ' + This.adapter.getView (i) + "</div>";}} document.getElementById (this.id).Innerhtml = txt;}}); Var myadapter = adapter.extend ({bean:[],ctor:function (_data) {this.bean = _data}, Getcount:function () {return this.bean.length;},getview:function (position) {var data = this.bean[position];var tmpl = "<div class = ' Row ' ><div class = ' col-xs-3 ' > Name </div><div class = ' col-xs-7 ' style = ' Text-align:right ' >${name}</div></div><div class = ' Row ' ><div class = ' col-xs-4 ' >${sex}</div><div class = ' col-xs-4 ' >${age}</div> <div class = ' col-xs-4 ' >${address}</div></div> '; while (Tmpl.indexof ("${") > 0) {var start = tmpl.indexof ("${"); Var end = tmpl.indexof ("}"); Var key = tmpl.substring (start,end+1); Tmpl = tmpl.replace (Key,data[key.replace ("${", " "). ReplaCE ("}", "")]);} Return tmpl;},getobject:function (position) {return bean[position];}}); <body> <button onclick = "LoadData ()" > Load Data </button> <div id = "ListView" > </div></ Body><script> var beans = [{name: ' Little Red ', Sex: ' Woman ', age:11,address: ' Xiamen "},{name: ' Xiao Ming ', Sex: ' Male ', age:12,address: ' Wenzhou '}]; var myadapter = null; var listview = null; (function () { myadapter = new myadapter (Beans); listview = new listview ("ListView"); listview.setadapter (myadapter); }); function loaddata () { &Nbsp; myadapter.datachanged (); }</script>
Run by:
650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/8B/7E/wKiom1hPlheyvz15AAAdns7esV4088.png-wh_500x0-wm_3 -wmp_4-s_3205063076.png "title=" adapter mode. png "alt=" wkiom1hplheyvz15aaadns7esv4088.png-wh_50 "/>
The advantage of the adapter mode is that the two unrelated classes are connected together to improve the reuse, as the above example ListView can be used in any situation, the data class can be used in any situation, the adapter will connect the two to the overall work to achieve the effect.
Too many adapters can cause confusion in the system, not easy to program readability.
This article from "Go one stop two look back three" blog, please make sure to keep this source http://janwool.blog.51cto.com/5694960/1882249
listview--Adapter mode for Android