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

Models Example

Adrián Rivero edited this page Mar 16, 2017 · 4 revisions

In this Example, let's create an User Model, injected in an activity and use the generated getter and setters.

This is out Model

@UseModel
public class User extends Model {
    String name;
    String email;
    int age;
}

Now we can inject this model in any Enhanced View, here is an example in an Enhanced Activity.

@EActivity(R.layout.activity_main)
public class EnhancedActivity extends Activity {
    @Model
    User_ user;

    @AfterInject
    void setUser() {
        user.setName("Alice");
        user.setEmail("alice@example.com");
        user.setAge(22);
    } 
}

or we can inject a List of these models, and instantiate a specific amount of them:

@EActivity(R.layout.activity_main)
public class EnhancedActivity extends Activity {

    @Model(query="count=10")
    List<User_> users;

    @AfterInject
    void setUsers() {
        for (int i=0; i < users.size(); i++) {
            User_ user = users.get(i);

            user.setName("User " + i);
            user.setEmail("user" + i + "@example.com");
            user.setAge(20+i);
        }
    } 
}