Skip to content
This repository has been archived by the owner on May 4, 2022. It is now read-only.

Populating Views

Adrián Rivero edited this page Feb 17, 2016 · 22 revisions

To populate a view with a model, we annotate the model injected field with @Populator.

@Model
@Populator
User user;

This will use the methods and fields from the model to populate the data in the user interface. In the user interface, the ID's should be assigned following one of the next rules:

  • The ID should start with the field name plus "_" plus the field or method name. Ex: user_name to populate with user.getName().
  • The ID should start with the field name and then the field or method name starting with a capital letter. Ex: userName to populate with user.getName().

To access to the model fields that are Models, it can be done following the same rules... let's say that the User model has a field named "partner" that it's also an User, and you want to populate a view with the "partner" name, then you assign the id in the layout to the view to be populated as user_partner_name.

You can Populate three different views:

  1. TextView and any of its subclasses (Ex. EditText). TextViews are populated using the method "setText()", the field that is gonna to be used is automatically cast to an String if it is required.
  2. ImageView and any of its subclasses. ImageViews are populated using Picasso library. You can pass as parameter an URL (it'll be downloaded in background by Picasso), an File object, a resource id (ex. R.drawable.image), or a file path.
  3. AdapterView and any of its subclasses (Ex. ListView, GridView), See Populating AdapterViews

Populating AdapterViews

To populate an adapter view you should assign an arbitrary ID to your AdapterView, then a field with the same name it is declared in your Enhanced Component (Ex. Activity or Fragment). You should provide to the @Populator a parameter indicating what layout to use for the elements of the AdapterViews.

@Model
@Populator(R.layout.item_users)
List<User> users;

Each of the views in the provided layout (R.layout.item_users in the example) should use one of the following rules:

  • The ID should start with the class name in lowercase plus "_" plus the field or method name. Ex: user_name to populate with user.getName() for each use of the list.
  • The ID should start with the field name and then the field or method name starting with a capital letter. Ex: userName to populate with user.getName() for each user of the list.

Populating different views

If you want to populate a different view, then you should declared a method named "assignField" in your Enhanced Component.

public void assignField(View view, Object value) {
    //Assign the parameter "value" to the parameter "view"
}

For instance, if we have a WebView and we want to Populated with a specific field of our injected model, we have to declare a method similar to this:

void assignField(View view, Object object) {
    if (view instanceof WebView) {
        ((WebView)view).loadData(object.toString(), "text/html; charset=utf-8", null);
    }
}

Populating with primitives or Strings

Primitives and Strings can also be Populators, to use this, you should assign an arbitrary ID to the View that you want populate, then you declare a field matching the same assign Id:

@UseFacebookSDK
@UseEventBus
@EActivity(R.layout.activity_login)
public class EnhancedActivity extends Activity {

    @Populator
    String text;

    @Populator
    int age;
}

Events and Actions in Model Injection with Populators

When a Populator is applied to a Model, if any Event and Action is used to Load or Update the view, this will result in upating the User Interface linked to the field by the Populator. So, in this example, every time that the event UpdateUIEvent is dispatched, all the views populated by the injected field user are going to be automatically upated as well.

@Model
@Populator
@UpdateOnEvent(UpdateUIEvent.class)
User user;

See also [Events and Actions in Model Injection](Events and Actions in Model Injection)

Populating properties

As a quick access to populate other properties of a view, you can specify the method name in the id followed by "$" and then the field to use.

For instance, if we want set the background color of a view depending of the age of an user, we can declare a method inside the user model that returns an int (the color), let's say this method is named "getBackground", then we can populate the background of a button simply assigning the Id for it setBackgroundColor$user_getBackground. This can be used with any method of the view, you should take care of returning the data that the specific method requires.

To assign a set of fields to more than one property of the same object, you can list all them in the Id following the same rule, for instance if we want to assign a text also to the same button, we can create a method for it in the model, let's say "getButtonText", and call it simply listing the method in the Id: setBackgroundColor$user_getBackground$setText$user_getButtonText.