어떻게 이것을 Android에서 Java로 리플렉션을 사용하여 몇 줄로 줄일 수 있습니까? 리플렉션을 사용하여 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 ...
나는 안드로이드 개발자가 아니기 때문에 미안하지만, 왜 'value'타입을 체크하고 있는가? 'this._properties.put (key, value);에 문제가 있습니까? – Pshemo
@Pshemo'ContentValues'는 일반적인'put 객체'메소드를 가지고 있지 않습니다. 내부 직렬화/소팅 때문에 특정 유형에 대한 타입이 지정된 메소드 만 있습니다 : 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