Android SQLite公用结果处理

看朋友写数据库查询,获取结果的时候,每次都会写一大堆代码,我想起以前Java里写过一个公用的方法。
因为初学Android所以就试着写一个,可以满足我的需求。
有什么需要改的或者优化的还请赐教。
代码如下:

package cn.com.choicesoft.weiqian.util;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 *@Author:M.c
 *@Comments:数据库公用查询结果 处理类
 *@CreateDate:2014-1-20
 *@Email:[email protected]
 */
public class ListProcessor {
	private DBManager db;
	public DBManager getDb(Context context) {
		if(db==null){
			db = new DBManager(context);
		}
		return db;
	}
	/**
	 * 根据传入实体返回对应的实体数组
	 * @param sql SQL语句
	 * @param selectionArgs SQL条件
	 * @param context Activity
	 * @param cal 实体类 【实体内属性只支持:String,Integer,Float,Double,Boolean,Short,Long,Character(char)】
	 * @return List<T>
	 */
	public <T> List<T> query(String sql,String[] selectionArgs,Context context,Class<T> cal){
		SQLiteDatabase database = getDb(context).openDatabase();
		Cursor c = database.rawQuery(sql, selectionArgs);
		Method[] methods = cal.getMethods();
		List<T> list=new ArrayList<T>();
		while (c.moveToNext()) {
			Object obj=null;
			try {
				obj=cal.newInstance();
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
			for (Method method : methods) {
				String methodName=method.getName();
				if(methodName.matches("^set[A-Z]\\w+$")){
					String name=methodName.substring(3, methodName.length()).toUpperCase();
					for (int i=0;i<c.getColumnCount();i++) {
						if(name.equalsIgnoreCase(c.getColumnName(i))){
							try {
								typeConf(method,c.getString(i),cal,obj);
								break;
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}
				}
			}
			list.add((T)obj);
		}
		if(c!=null){
			c.close();
		}
		if(database!=null){
			database.close();
		}
		return list;
	}
	/**
	 * 类型转换
	 * @param method
	 * @param value
	 * @param cla
	 * @throws Exception
	 */
	public void typeConf(Method method, Object value, Class<?> cls,Object obj)	throws Exception {
		Object typeName = method.getParameterTypes()[0];
		Method setMethod = null;
		if (typeName == String.class) {
			setMethod = cls.getMethod(method.getName(), String.class);
			setMethod.invoke(obj, value.toString());
		}else if (typeName == Double.class||typeName.toString().equals("double")){
			setMethod = cls.getMethod(method.getName(), typeName == Double.class?Double.class:double.class);
			setMethod.invoke(obj, new Double(value.toString()));
		}else if (typeName == Integer.class||typeName.toString().equals("int")) {
			setMethod = cls.getMethod(method.getName(), typeName == Integer.class?Integer.class:int.class);
			setMethod.invoke(obj, Integer.valueOf(value.toString()));
		}else if (typeName == Long.class||typeName.toString().equals("long")) {
			setMethod = cls.getMethod(method.getName(), typeName == Long.class?Long.class:long.class);
			setMethod.invoke(obj, Long.valueOf(value.toString()));
		} else if (typeName == Character.class||typeName.toString().equals("char")) {
			setMethod = cls.getMethod(method.getName(),typeName == Character.class?Character.class:char.class);
			setMethod.invoke(obj, value.toString().charAt(0));
		} else if (typeName == Boolean.class||typeName.toString().equals("boolean")) {
			setMethod = cls.getMethod(method.getName(), typeName == Boolean.class?Boolean.class:boolean.class);
			setMethod.invoke(obj, new Boolean(value.toString()));
		} else if (typeName == Float.class||typeName.toString().equals("float")) {
			setMethod = cls.getMethod(method.getName(), typeName == Float.class?Float.class:float.class);
			setMethod.invoke(obj, Float.valueOf(value.toString()));
		}  else if (typeName == Short.class||typeName.toString().equals("short")) {
			setMethod = cls.getMethod(method.getName(), typeName == Short.class?Short.class:short.class);
			setMethod.invoke(obj, Short.valueOf(value.toString()));
		}
	}



声明
        欢迎转载,但请保留文章原始出处
        [Iteye]- http://jnwsczh.iteye.com/blog/2019454

猜你喜欢

转载自jnwsczh.iteye.com/blog/2019454