I can not read the values of the class attributes with reflection:
ContentValues values = new ContentValues();
Field[] fields = this.getClass().getDeclaredFields();
for (Field field: fields ) {
field.setAccessible(true);
values.put(field.getName(), field.get(this.getClass()));
}
I have the exception: IllegalAccessException
in the get method: field.get(this.getClass())
I also tried field.get(this)
and still passing the instance of object field.get(model)
The attributes of the model object are not private. If you do not use the field.setAccessible(true);
method, the exception is the same.
At the request of your friend Ramaral, follow the whole class:
public class GenericModel {
public Long id;
public ContentValues GetContentValues()
{
ContentValues values = new ContentValues();
Field[] fields = this.getClass().getDeclaredFields();
for (Field field: fields ) {
field.setAccessible(true);
try {
values.put(field.getName(), field.get(this.getClass()));
} catch (IllegalAccessException e) {
}
}
return values;
}
}