/** * @param view the parent view where the held views reside */ public ViewHolder(View view) { mView = view; FieldAnnotationParser.setViewFields(this, view); }
/** * Constructs a ActivityViewHolder using an activity * * @param activity the Activity where the held views reside */ public ActivityViewHolder(Activity activity) { mActivity = activity; FieldAnnotationParser.setViewFields(this, activity); }
/** * Parse {@link ViewId} annotation and try to assign the view with that id to the annotated field. * It will throw a {@link ClassCastException} if the field and the view with the given ID have different types. * * @param object object where the annotation is. * @param view parent view that contains a view with the viewId given in the annotation. */ public static void setViewFields(final Object object, final View view) { setViewFields(object, new ViewFinder() { @Override public View findViewById(int viewId) { return view.findViewById(viewId); } }); }
/** * Parse {@link ViewId} annotation and try to assign the view with that id to the annotated field. * It will throw a {@link ClassCastException} if the field and the view with the given ID have different types. * * @param object object where the annotation is. * @param activity activity that contains a view with the viewId given in the annotation. */ public static void setViewFields(final Object object, final Activity activity) { setViewFields(object, new ViewFinder() { @Override public View findViewById(int viewId) { return activity.findViewById(viewId); } }); }
@Test public void testSetViewFields() throws Exception { TestObjectWithAnnotations testObject = new TestObjectWithAnnotations(); FieldAnnotationParser.setViewFields(testObject, createTestLinearLayout()); Assert.assertNotNull(testObject.textView); Assert.assertNotNull(testObject.imageView); }
@Test public void testSetViewFieldsActivity() throws Exception { Activity testActivity = Robolectric.buildActivity(TestActivity.class).create().get(); TestObjectWithAnnotations testObject = new TestObjectWithAnnotations();; FieldAnnotationParser.setViewFields(testObject, testActivity); Assert.assertNotNull(testObject.textView); Assert.assertNotNull(testObject.imageView); }