Android Universal Adapter

Source: Internet
Author: User

Each time you create a collection, you create an adapter that creates a view each time because the structure of the data is different.

Here I have created an adapter that can be used for data loading, regardless of the structure of the data.

Package Org.adapter;import Java.lang.reflect.method;import Java.util.list;import java.util.vector;import Org.asynctask.asynctaskmanager;import Org.asynctask.showimgaelater;import Org.tourists.r;import Org.utils.stringutils;import Org.utils.transformutils;import Android.app.activity;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.baseadapter;import Android.widget.imageview;import Android.widget.textview;public class DataAdapter Extends Baseadapter {protected string[] keys; protected int[]resids;protected Activity context; protected int layout; p rotected Layoutinflater Layoutinflater;  Public list<?> datas; Public DataAdapter (Activity context, list<?> datas,string[] keys, int[] resids, int layout) {This.keys = Keys;this. Resids = Resids;this.context = Context;this.layout = Layout;this.layoutinflater = Layoutinflater.from (context); This.datas = datas;} @Overridepublic int GetCount () {return datas.size ();} @OverRidepublic Object getItem (int position) {return datas.get (position);} @Overridepublic long Getitemid (int position) {return ToInt (Invoke (GetItem (position), "id")) @Overridepublic View Getv Iew (int position, View Convertview, ViewGroup parent) {if (null = = Convertview) {Convertview = Layoutinflater.inflate ( layout, NULL); }else{int uppostion = Transformutils.toint (Convertview.gettag ()); if (uppostion = = position) {return convertview;}} Object target = GetItem (position); for (int x=0;x<resids.length;x++) {View view = Convertview.findviewbyid (Resids[x]); String key = Keys[x]; String value = Invoke (target, key); if (view instanceof TextView) {TextView TextView = (TextView) view; Textview.settext (value);} else if (view instanceof ImageView) {ImageView ImageView = (ImageView) view; Imageview.setimageresource (r.drawable.no_ Show); Asynctaskmanager.addtaskdao (New Showimgaelater ()). Execute (imageView, value);} else{//other view}}convertview.settag (position); return convertview; }/** * Get method and invoke this method * @param target * @param fieldName * */protected string Invoke (Object target, string FieldName) {GetMethods (Target.getclass ()); Method currentmethod = null; for (Method method:methods) {if ("Get". Equals (Method.getname ())) {if (Method.getparametertypes (). length = = 1) { Currentmethod = method;}}} if (null = = Currentmethod) {String methodName = "Get" + stringutils.tofristupper (fieldName); for (Method method:methods) {i F (Methodname.equalsignorecase (Method.getname ())) {if (Method.getparametertypes (). Length = = 0) {Currentmethod = method ;}}} Return toString (Methodinvoke (target, Currentmethod));} Else{try {return (String) Currentmethod.invoke (target, FieldName),} catch (Exception e) {e.printstacktrace ();} return to String (Methodinvoke (Target, Currentmethod, fieldName)); }}/** * Method Invoke * @param target * @param method * @param args * */protected object Methodinvoke (Object target , method, Object...args) {try {return Method.invoke (target, args); } catch (Exception e) {}return null; }list<method> methods = new vector<method> ();/** * Get class All Method * @param class * */protected void Getm Ethods (class<?> clazz) {if (Object.class.equals (Clazz)) {return;} Method[]tempmethods = Clazz.getdeclaredmethods (); for (method Method:tempmethods) {Methods.add (method);} GetMethods (Clazz.getsuperclass ()); }/** * obj to String * */public string toString (Object value) {return NULL = = value? "": value.tostring (). Trim ();} public int toint (Object obj) {if (obj = = null) {return 0;} if (obj instanceof number) {Number number = (number) Obj;return number.intvalue ();} String value = toString (obj); try {return integer.parseint (value);} catch (Exception e) {}return 0;}}
</pre></p><p></p> the adapter calls the following method: <pre name= "code" class= "java" >listview lv_scenic_show = ( ListView) Findviewbyid (r.id.lv_scenic_show);  Lv_scenic_show.setadapter (New DataAdapter (this, points,new string[]{"cover", "Picturecount", "Audiocount", "distance "," Sname "}, New Int[]{r.id.iv_scenic_item_main, R.id.tv_scenic_pic, R.id.tv_scenic_mp3, R.id.tv_scenic_distance, R.i D.tv_scenic_panorama_scenics_name}, R.layout.scenic_panorama_item));  



Android Universal Adapter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.