mercredi 21 septembre 2016

Acces Views with Reflected Class

Here is a custom annotation:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Sample {
    String value() default "default";
}

and this is when it is being used:

@SharedView(key = "sample")
    public TextView helloTxt;

and I am trying to place the view in a hashmap so it can be used later.

BiMap<String, View> viewBind = HashBiMap.create();

public void sampleTest(Object target, Context context) {

        Class<?> obj = target.getClass();
        for (Field field : obj.getDeclaredFields()) {
            if (field.isAnnotationPresent(Sample.class)) {
                Annotation annotation = field.getAnnotation(Sample.class);
                Sample sampleView = (Sample) annotation;

//                viewBind.put(sampleView.value(), ((View) field.getGenericType()));
                Log.e("result", field.getGenericType().toString());

            }
        }
    }

How can I save a reference to the view so operation like setText() can later on it. Thanks





Aucun commentaire:

Enregistrer un commentaire