A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 陈君 金牌黑马   /  2014-9-17 18:21  /  1393 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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

  2. import java.lang.reflect.Method;
  3. import java.util.ArrayList;
  4. import java.util.List;

  5. import <a title="android" href="http://www.android-study.com/jichuzhishi/567.html">android</a>.content.Context;
  6. import <a title="android" href="http://www.android-study.com/jichuzhishi/567.html">android</a>.database.Cursor;
  7. import <a title="android" href="http://www.android-study.com/jichuzhishi/567.html">android</a>.database.<a title="sqlite" href="http://www.android-study.com/jichuzhishi/567.html">sqlite</a>.SQLiteDatabase;

  8. /**
  9. *@Author:M.c
  10. *@Comments:数据库公用查询结果 处理类
  11. *@CreateDate:2014-1-20
  12. *@Email:JNWSCZH@163.COM
  13. */
  14. public class ListProcessor {
  15. private DBManager db;
  16. public DBManager getDb(Context context) {
  17. if(db==null){
  18. db = new DBManager(context);
  19. }
  20. return db;
  21. }
  22. /**
  23. * 根据传入实体返回对应的实体数组
  24. * @param sql SQL语句
  25. * @param selectionArgs SQL条件
  26. * @param context Activity
  27. * @param cal 实体类 【实体内属性只支持:String,Integer,Float,Double,Boolean,Short,Long,Character(char)】
  28. * @return List<T>
  29. */
  30. public <T> List<T> query(String sql,String[] selectionArgs,Context context,Class<T> cal){
  31. SQLiteDatabase database = getDb(context).openDatabase();
  32. Cursor c = database.rawQuery(sql, selectionArgs);
  33. Method[] methods = cal.getMethods();
  34. List<T> list=new ArrayList<T>();
  35. while (c.moveToNext()) {
  36. Object obj=null;
  37. try {
  38. obj=cal.newInstance();
  39. } catch (InstantiationException e) {
  40. e.printStackTrace();
  41. } catch (IllegalAccessException e) {
  42. e.printStackTrace();
  43. }
  44. for (Method method : methods) {
  45. String methodName=method.getName();
  46. if(methodName.matches("^set[A-Z]\w+$")){
  47. String name=methodName.substring(3, methodName.length()).toUpperCase();
  48. for (int i=0;i<c.getColumnCount();i++) {
  49. if(name.equalsIgnoreCase(c.getColumnName(i))){
  50. try {
  51. typeDivision(method,c.getString(i),cal,obj);
  52. break;
  53. } catch (Exception e) {
  54. e.printStackTrace();
  55. }
  56. }
  57. }
  58. }
  59. }
  60. list.add((T)obj);
  61. }
  62. if(c!=null){
  63. c.close();
  64. }
  65. if(database!=null){
  66. database.close();
  67. }
  68. return list;
  69. }
  70. /**
  71. * 不同类型转换
  72. * @param method
  73. * @param value
  74. * @param cla
  75. * @throws Exception
  76. */
  77. public void typeDivision(Method method, Object value, Class<?> cls,Object obj) throws Exception {
  78. Object typeName = method.getParameterTypes()[0];
  79. Method setMethod = null;
  80. if (typeName == String.class) {
  81. setMethod = cls.getMethod(method.getName(), String.class);
  82. setMethod.invoke(obj, value.toString());
  83. }else if (typeName == Double.class||typeName.toString().equals("double")){
  84. setMethod = cls.getMethod(method.getName(), typeName == Double.class?Double.class:double.class);
  85. setMethod.invoke(obj, new Double(value.toString()));
  86. }else if (typeName == Integer.class||typeName.toString().equals("int")) {
  87. setMethod = cls.getMethod(method.getName(), typeName == Integer.class?Integer.class:int.class);
  88. setMethod.invoke(obj, Integer.valueOf(value.toString()));
  89. }else if (typeName == Long.class||typeName.toString().equals("long")) {
  90. setMethod = cls.getMethod(method.getName(), typeName == Long.class?Long.class:long.class);
  91. setMethod.invoke(obj, Long.valueOf(value.toString()));
  92. } else if (typeName == Character.class||typeName.toString().equals("char")) {
  93. setMethod = cls.getMethod(method.getName(),typeName == Character.class?Character.class:char.class);
  94. setMethod.invoke(obj, value.toString().charAt(0));
  95. } else if (typeName == Boolean.class||typeName.toString().equals("boolean")) {
  96. setMethod = cls.getMethod(method.getName(), typeName == Boolean.class?Boolean.class:boolean.class);
  97. setMethod.invoke(obj, new Boolean(value.toString()));
  98. } else if (typeName == Float.class||typeName.toString().equals("float")) {
  99. setMethod = cls.getMethod(method.getName(), typeName == Float.class?Float.class:float.class);
  100. setMethod.invoke(obj, Float.valueOf(value.toString()));
  101. } else if (typeName == Short.class||typeName.toString().equals("short")) {
  102. setMethod = cls.getMethod(method.getName(), typeName == Short.class?Short.class:short.class);
  103. setMethod.invoke(obj, Short.valueOf(value.toString()));
  104. }
  105. }
  106. }
复制代码


1 个回复

倒序浏览

顶一个。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马