Adapters are very common in Android, like Listview,gridview, drop-down boxes ... Are more commonly used,
This blog introduces a universal adapter, although relatively simple, but the use is relatively large. (Do not create the adapter again)
This is a virtual class that you need to inherit from and implement the appropriate method.
Public abstract class Simplebaseadapter<t> extends baseadapter{//Call class Incoming context context//required data, using pan Type List<t> data; Construction method, incoming context and data public simplebaseadapter (context context, list<t>data) {this.context = context; This.data = data = = null? New Arraylist<t> (): data; } @Override public int getcount () {return data.size (); } @Override public Object getItem (int position) {if (position >= data.size ()) {return n Ull } return Data.get (position); } @Override public long getitemid (int position) {return position; }//Inherit the method that the class needs to implement, get the layout ID of the list item public abstract int getitemrsouce (); Inherits the method that the class needs to implement, gets the list item's view public abstract view Getitemview (int position,view convertview,viewholder holder); The most important method in the adapter is to return to the View call Getitemview () This method//Because the child view in the returned view needs to be confirmed @SuppressWarnings ("unchecked") according to the ID of the view in the implementation class @Ov Erride Public View GetvIew (int position, View Convertview, ViewGroup parent) {Viewholder holder; if (Convertview = = null) {Convertview = View.inflate (context, getitemrsouce (), NULL); Holder = new Viewholder (Convertview); Convertview.settag (holder); } else{holder = (viewholder) convertview.gettag (); } return Getitemview (position, Convertview, Holder); }//A new Viewholder writing, the reason for this is to facilitate//implement class get Convertview in the sub-view, Holderview caching mechanism//use Sparsearray to cache the public class viewholder{private sparsearray<view> views = new sparsearray<view> (); Private View Convertview; Public Viewholder (View convertview) {this.convertview = Convertview; } @SuppressWarnings ("Unchecked") public <t extends view> T getView (int resId) {View v = VI Ews.get (RESID); if (v = = null) {v = Convertview.findviewbyid (resId); Views.Put (ResId, v); } return (T) v; } }}Simplebaseadapter is actually an abstraction of the adapter, and the different manifestations of this abstract class are abstracted
Implementation, because the implementation class must implement an abstract method (or it can be said that the implementation class behaves differently depending on the abstract method)
Simplebaseadapter Implementation class:
public class Myadapter extends simplebaseadapter<string> {public myadapter (Context context,list<string > Data) { Super (context, data); } @Override public int getitemrsouce () { return r.layout.list_item; Returns a list of the secondary implementation classes item's XML file ID } //Get View by ID (the ID of each implementation class is different) @Override public View getitemview (final int Position, View Convertview, Viewholder holder) { TextView TV = Holder.getview (r.id.title); Tv.settext (String) GetItem (position)); Button btn = Holder.getview (r.id.btn); Btn.settext ("select"); Btn.setonclicklistener (New View.onclicklistener () { @Override public void OnClick (View v) { Toast.maketext (MyAdapter.this.context, "" + Position, Toast.length_short). Show (); } ); return convertview;} }
Custom Simple Universal Adapter