Question

Can't get @ViewById to work inside an @EBean.

@EActivity(R.layout.data_layout)
public class MyActivity extends Activity {
   @Bean
   MyEbean bean;

   @AfterViews
   public void setupView() {
     bean.loadData("Test name");
   }
}

@EBean
public class MyEbean {
   @RootContext
   context;

   @ViewById(R.id.name_field)
   TextView nameField;

  public void loadData(String name) {
    nameField.setText(name); 
  }
}

"nameField" in loadData() is null. At the same time if I do this inside loadData()

 nameField = (TextView)((Activity) context).findViewById(R.id.name_field);

it's all good. So the view is definitely there. Also if I call this method from a retrofit callback (i.e. after a delay) "name" is auto populated. Anything that I'm doing wrong there?

I'm using Android Annotations 3.0.1.

Était-ce utile?

La solution

@EBean annotated classes aren't supposed to do any view-related work as they can be injected in every enhanced classes (ie annotated with @EService, @EBean, @EActivity, ...).

If you're able to do a findViewById in the context retrieved via @RootContext, it's only because – in this case – the context given to the bean IS the activity. But depending of where the bean is injected, it could also be the ApplicationContext or a Context linked to another acivity/layout.

Autres conseils

@Leo here is an implementation of a custom view and binding data to it. I wanted to make a widget like chips

http://www.google.com/design/spec/components/chips-tokens.html

First define the layout in xml

<?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:id="@+id/ivIcon"
    android:layout_width="@dimen/chip_size"
    android:layout_height="@dimen/chip_size"
    android:src="@drawable/default_icon" />

<TextView
    android:id="@+id/tvName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginLeft="8dp"
    android:layout_toRightOf="@+id/ivIcon"
    android:fontFamily="sans-serif"
    android:lines="1"
    android:singleLine="true"
    android:textColor="@color/secondary_text"
    android:textSize="14sp" />

Create a class and annotate it with @eviewgroup ChipView.java

@EViewGroup(R.layout.layout_chip)
public class ChipView extends RelativeLayout{

@App
App app;

public ChipView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

}

public ChipView(Context context, AttributeSet attrs) {
    super(context, attrs);

}

public ChipView(Context context) {
    super(context);
}

@ViewById(R.id.layoutChipContainer)
View container;

@ViewById(R.id.ivIcon)
ImageView ivIcon;

@ViewById(R.id.tvName)
TextView tvName;

public void bind(User user){

    tvName.setText(user.getUsername());

    app.imageLoader.displayImage(user.getPicture(), ivIcon, app.myResources.iconOptions);
}

}

Then in your activity

ChipView v = ChipView_.build(context);

v.bind(user);

where context is some sort of context and user is an POJO

super easy.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top