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:JNWSCZH@163.COM
*/
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()));
}
}



[color=red][b]声明[/b][/color]
欢迎转载,但请保留文章原始出处
[Iteye]-[url]http://jnwsczh.iteye.com/blog/2019454[/url]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值