Created
March 29, 2015 05:44
-
-
Save achuinard/60903f423cb37ea8c544 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FirebaseDataSnapshotMapper { | |
public static void map(Object object, DataSnapshot dataSnapshot) { | |
try { | |
final Class<?> aClass = object.getClass(); | |
final Field[] fields = aClass.getDeclaredFields(); | |
for (final Field classField : fields) { | |
if (classField.isAnnotationPresent(Firemapped.class)) { | |
classField.setAccessible(true); | |
final Firemapped annotation = classField.getAnnotation(Firemapped.class); | |
final DataSnapshot child = dataSnapshot.child(annotation.firebasePath()); | |
if (child != null) { | |
final Class fieldClassType = classField.getType(); | |
if (fieldClassType == EditText.class) { | |
if (annotation.valueType() == String.class) { | |
((EditText) classField.get(object)).setText(child.getValue(String.class)); | |
} else if (annotation.valueType() == Integer.class) { | |
final Integer value = child.getValue(Integer.class); | |
((EditText) classField.get(object)).setText(String.valueOf(value)); | |
} | |
} | |
} | |
} | |
} | |
} catch (Exception e) { | |
Log.e(FirebaseDataSnapshotMapper.class.getName(), "Error mapping Firebase object", e); | |
} | |
} | |
} | |
@Retention(RetentionPolicy.RUNTIME) | |
public @interface Firemapped { | |
public String firebasePath(); | |
public Class valueType() default String.class; | |
} | |
@Firemapped(firebasePath = "first_name", valueType = String.class) | |
EditText mFirstName; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment