我该如何减少这一点在java中用于Android的反射几行?使用反射来投射一个对象在java中为Android
(_properties是ContentValues对象和值是一个对象)
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 ...
我不是Android开发人员,所以我为我的无知感到抱歉,但为什么你甚至检查'value'类型? 'this._properties.put(key,value)是否有问题;'? – Pshemo
@Pshemo'ContentValues'没有一个通用的'put object'方法,只有某些类型的类型化方法(因为内部序列化/ parcel):http://developer.android.com/reference/android/content/ContentValues .html当使用这些时,最好是转换而不是转换为一个字符串,但这会减少只需稍微使用的代码(例如'this._properties.put(key,(Byte)value)')。 –
'this._properties.put(key,(Byte)value))'与this._properties.put(key,Byte.valueOf(value.toString()))'是一样的,对吧? – spacebiker