码迷,mamicode.com
首页 > 移动开发 > 详细

android 通用适配器

时间:2014-12-03 12:30:49      阅读:205      评论:0      收藏:0      [点我收藏+]

标签: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;
	}

	@Override
	public int getCount() {
		return datas.size() ;
	}

	@Override
	public Object getItem(int position) {
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return toInt(invoke(getItem(position), "id") ); 
	} 

	@Override
	public 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 通用适配器

标签:android   blog   io   ar   os   使用   java   for   on   

原文地址:http://blog.csdn.net/hfmbook/article/details/41694307

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!