dimanche 9 juillet 2017

I want to get the field name of class in android

I want to get the field name of class in android. for this I am using this code

public static String checkForNull(Object obj) {
    Class cls = obj.getClass();
    Field fields[] = cls.getDeclaredFields();
    for (Field field : fields) {
        Object object = null;
        try {
            object = field.get(obj);
        } catch (IllegalAccessException e) {
            return "The field " + field.getName() + " of " + cls.getSimpleName() + " is not accessible!";
        }
        if (object == null) {
            return "The field " + field.getName() + " of " + cls.getSimpleName() + " is null!";
        } else if (object instanceof String) {
            String string = (String) object;
            if (string.equals("")) {
                return "The field " + field.getName() + " of " + cls.getSimpleName() + " is empty!";
            }
        }
    }
    return null;
}

but this code return generic name of class field like a,b,c...etc. This code work fine in netbeans.





Aucun commentaire:

Enregistrer un commentaire