sqlite查询数据库数据cursor转list封装
public static List CursorToList(Cursor cursor,MyObject bean){List resultList=new ArrayList();if(cursor!=null){String type="";String name="";HashMap,String> hashMap
·
这里面只写了几种属性判断,如果你有其他属性也可以继续做判断。 //使用样例:实体 bean=new 实体();List list=CursorToList(cursor,bean); // public static List CursorToList(Cursor cursor,MyObject bean){ List resultList=new ArrayList(); if(cursor!=null){ String type=""; String name=""; HashMap<String,String> hashMap=new HashMap<>(); List<HashMap<String,String>> hashMapList=getFiledsInfo(bean.getClass()); while (cursor.moveToNext()){ MyObject bean1;bean1=(MyObject)bean.clone();
for (int i = 0; i < hashMapList.size(); i++) {
hashMap=hashMapList.get(i);
type=hashMap.get("type");
name=hashMap.get("name");
if("class java.lang.Integer".equals(type) || "int".equalsIgnoreCase(type)){
int typeResult=cursor.getInt(cursor.getColumnIndex(name));
bean1=setFieldValueByName(bean1,name,"int",typeResult);
}else if("class java.lang.String".equals(type)){
String typeResult=cursor.getString(cursor.getColumnIndex(name));
bean1=setFieldValueByName(bean1,name,"string",typeResult);
}else if("class java.lang.Long".equals(type)){
long typeResult=cursor.getLong(cursor.getColumnIndex(name));
bean1=setFieldValueByName(bean1,name,"long",typeResult);
}
}
resultList.add(bean1);
}
}
return resultList;
}
/**
* 获取属性类型(type),属性名(name)的map组成的list
*
*/
private static List getFiledsInfo(Class<?> o){
Field[] fields=o.getDeclaredFields();
List list = new ArrayList();
Map infoMap=null;
for(int i=0;i<fields.length;i++){
if(!fields[i].isSynthetic() && !"serialVersionUID".equals(fields[i].getName())) {
infoMap = new HashMap();
infoMap.put("type", fields[i].getType().toString());
infoMap.put("name", fields[i].getName());
list.add(infoMap);
}
}
return list;
}
/**
* 根据属性名赋予属性值
*
*/
private static MyObject setFieldValueByName( MyObject o,String fieldName,String types,Object values) {
if(o!=null)
try {
String firstLetter = fieldName.substring(0, 1).toUpperCase();
String getter = "set" + firstLetter + fieldName.substring(1);
Method method=null;
if("int".equalsIgnoreCase(types))//Integer和int不同,如果object里面用的是int,这里就用int.class
method = o.getClass().getMethod(getter,int.class);
else if("string".equalsIgnoreCase(types))
method = o.getClass().getMethod(getter,String.class);
else if("long".equalsIgnoreCase(types))
method = o.getClass().getMethod(getter,long.class);
method.invoke(o, values);
} catch (Exception e) {
}
return o;
}/** * 实现可克隆功能的Object类 * */ public class MyObject extends Object implements Cloneable{ @Override public Object clone(){ MyObject sc = null; try { sc = (MyObject) super.clone(); } catch (CloneNotSupportedException e){ e.printStackTrace(); } return sc; } }
更多推荐
已为社区贡献1条内容
所有评论(0)