Android学习之——项目中的反射学习
/** * 利用反射确定是否关闭对话框 * * @param dialog * @param close * false:不关闭;true:关闭 */ public void closeDialog(final DialogInterface dialog, boolean close) { // 利用反射使点击按钮时,对话框不会关闭 try { // 得到AlertDialog的父类属性mShowing field = dialog.getClass().getSuperclass() .getDeclaredField("mShowing"); field.setAccessible(true); // 将mShowing变量设为false,表示对话框已关闭 field.set(dialog, close); dialog.dismiss(); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } }
/** * 得到cla类的属性列表 * * @param cla * 要反射的类 * @return */ @SuppressWarnings("rawtypes") public List<String> getPropertyNames(Class cla) { List<String> list = new ArrayList<String>(); Field[] fs = cla.getDeclaredFields(); // fs=cla.getFields();加了这个的话就只获得public 公有的 for (Field f : fs) { list.add(f.getName()); } return list; }
/**
* 得到与属性列表相对应的属性值列表
*
* @param shuxingList
* 属性列表
* @param obj
* 实体类
* @return 与属性对应的属性值列表
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public List getValueList(List<String> shuxingList, Object obj) {
List valueList = null;
try {
Class cla = obj.getClass();
valueList = new ArrayList();
for (int i = 0; i < shuxingList.size(); i++) {
Field f = cla.getDeclaredField(shuxingList.get(i).toString());
f.setAccessible(true);// 加了这句才能改私有的值
// 得到属性值
Object str = f.get(obj);
valueList.add(str);
}
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return valueList;
}
/** * 将obj类的属性列表一个个赋值 * * @param obj * 要反射的类 * @param propertyNames * 类的属性列表 * @param propertyVales * 与属性相对应的属性值列表 * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalArgumentException * @throws IllegalAccessException */ @SuppressWarnings("rawtypes") public void method(Object obj, List<String> propertyNames, List propertyVales) { try { Class cla = obj.getClass(); for (int i = 0, len = propertyNames.size(); i < len; i++) { // Log.i("propertyNames"+i, propertyNames.get(i)+""); Field f = cla.getDeclaredField(propertyNames.get(i).toString()); f.setAccessible(true);// 加了这句才能改私有的值 f.set(obj, propertyVales.get(i)); } } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchFieldException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } }d. 通过反射得到字段名称
public String[] GetFiled(Object object) { Class<? extends Object> cla = object.getClass(); Field[] fs = cla.getDeclaredFields(); String[] fields = new String[fs.length]; for (int i = 0; i < fs.length; i++) { // Log.i("propertyNames"+i, propertyNames.get(i)+""); fields[i] = fs[i].getName(); } return fields; }e. 通过反射给obj类的单个属性赋值
/** * 给obj类的单个属性赋值 * * @param obj * 要反射的类 * @param shuXing * 要赋值的属性 * @param value * 要给属性赋予的值 * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalArgumentException * @throws IllegalAccessException */ public void method(Object obj, String shuXing, Object value) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { @SuppressWarnings("rawtypes") Class cla = obj.getClass(); Field f = cla.getDeclaredField(shuXing); // 加了这句才能改私有的值 f.setAccessible(true); // 为属性赋值 f.set(obj, value); }
/** * 对类解析并赋值 * * @param documentElement * @param object * @return * @throws NoSuchFieldException * @throws IllegalAccessException * @throws InstantiationException * @throws ClassNotFoundException */ public Object useFanSheToData(SoapObject documentElement, Object object,String entityname) throws NoSuchFieldException, IllegalAccessException, InstantiationException, ClassNotFoundException { // 利用反射给类赋值 FanShe fanShe = new FanShe(); // 存放类的属性 List<String> classFileList = new ArrayList<String>(); // 存放类的属性值 List<Object> classValueList = new ArrayList<Object>(); // 得到类反射类的属性名称 Object obj = null; // 得到数据的key和value for (int i = 0; i < documentElement.getPropertyCount(); i++) { PropertyInfo propertyInfo = new PropertyInfo(); documentElement.getPropertyInfo(i, propertyInfo); String propertyName = propertyInfo.getName(); // System.out.println(propertyName); if (documentElement.getProperty(propertyName).toString() .startsWith("anyType")) { if (documentElement.getProperty(propertyName).toString() .equals("anyType{}")) { classFileList.add(propertyName); classValueList.add(""); } else { obj = useFanSheToData( (SoapObject) documentElement .getProperty(propertyName), Class.forName(entityname + propertyName) .newInstance(),entityname); classFileList.add(propertyName); classValueList.add(obj); } } else { classFileList.add(propertyName); classValueList.add(documentElement.getProperty(propertyName) .toString()); } } fanShe.method(object, classFileList, classValueList); return object; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。