dimanche 25 novembre 2018

How to get Field name of particular object in java android?

I want to get Field name of an Object.

public class Bike {

    String bikeModel;
    Int price;
}

I want to get the field name of bikeModel; I know that this way I can get all fields but I want only particular field name. Method of getting all fields

 public static ArrayList<String> getFieldsName(Class o) {
            ArrayList<String> cv = new ArrayList<String>();
            for (Field field : o.getFields()) {
                cv.add(field.getName());
            }
            return cv;
        }

This way I can get Field name not I don't want this

 try {
     Class<Bike> types = Bike.class;
     java.lang.reflect.Field field = types.getDeclaredField("bikeModel");
     field.setAccessible(true);
     String name= field.getName();

 } catch (Exception e) {
     e.printStackTrace();
 }

I want to get from Bike.bikeModel. Suppose anyhow rename bikeModel then above method will not work.





Aucun commentaire:

Enregistrer un commentaire