[英]Using reflection to cast an object in java for android
How can i reduce this to a couple of lines using reflection in java for android? 如何在Java for Android中使用反射将其减少到几行?
( _properties is a ContentValues object and value is an object) ( _properties是ContentValues对象,而value是一个对象)
if (value instanceof String)
{
this._properties.put( key, value.toString());
} else if (value instanceof Long) {
this._properties.put( key, Long.valueOf(value.toString()));
} else if (value instanceof Integer) {
this._properties.put( key, Integer.valueOf(value.toString()));
} else if (value instanceof Boolean) {
this._properties.put( key, Boolean.valueOf(value.toString()));
} else if (value instanceof Byte) {
this._properties.put( key, Byte.valueOf(value.toString()));
} else ...
No reflection needed: 无需反射:
_properties.put(key, value.toString());
Unfortunately ContentValues
does not have put(String, Object)
even though internally the values are stored in HashMap<String, Object>
. 不幸的是put(String, Object)
即使ContentValues
在内部将值存储在HashMap<String, Object>
put(String, Object)
也没有put(String, Object)
。
Why storing values as String
works is that all ContentValues
getAsFoo()
accessors support converting to Foo
from String
. 为什么将值存储为String
是所有ContentValues
getAsFoo()
访问器都支持从String
转换为Foo
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.