標籤:android blog io ar os 使用 java for on
每次建立一個集合,都要建立 一個適配器,每次每次都因為資料的結構不一樣而建立一個視圖。
這裡我建立了一個適配器,不管資料什麼結構,都能使用該適配器進行資料裝載。
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 ; protected 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 getView(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){if(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 toString(methodInvoke(target, currentMethod , fieldName ) ) ; }}/** * method invoke * @param target * @param method * @param args * */protected Object methodInvoke(Object target , Method 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 getMethods(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>該適配器調用方法如下:<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.id.tv_scenic_panorama_scenics_name }, R.layout.scenic_panorama_item ) ) ;
android 通用適配器