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 Mar 28, 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 layout, the IDs should be assigned following one of the next rules:

  • The ID should start with the field name plus the symbol "_" 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.

DecleX supports populating by default these views:

  1. TextView and any of its subclasses (Ex. EditText). TextViews are populated using the method setText(String), the field that is going to be used is automatically cast to a 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), a File object, a resource id (ex. R.drawable.image) or a file path.
  3. AdapterView and any of its subclasses (Ex. ListView, GridView, Spinner, Gallery), See Populating AdapterViews and RecyclerViews
  4. RecyclerView and any of its subclases, See Poppulating AdapterViews and RecyclerViews

Populating AdapterViews and RecyclerView

To populate an AdapterView (ListView, GridView, Spinner, Gallery) or a RecyclerView you should assign an arbitrary ID to it, then a field with the same name it is declared in your Enhanced Component (Ex. Activity or Fragment). You should provide in the layout the layout to use for the items in the tools:listitem attribute.

@Model
@Populator
List<User> users;
<android.support.v7.widget.RecyclerView
        android:id="@+id/users"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:listitem="@layout/item_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 the symbol "_" plus the field or method name. Ex: user_name to populate with user.getName() for each user in the list.
  • The ID should start with the class name in lowercase and then the field or method name starting with a capital letter. Ex: userName to populate with user.getName() for each user in the list.

Populating different views

If you want to populate a different view, then you should declare 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 populate 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 populated, to use this, you should assign an arbitrary ID to the View that you want to populate, then you declare a field matching the same Id:

@EActivity(R.layout.activity_enhanced)
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 or Action is used to Load or Update the model, 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.