Really I see that there is nothing like Mysql to be able to assign auto-increase of an index. How could something similar be done when entering new records.
Structure User
extended RealmObject
public class User extends RealmObject {
public static final String ID = "id";
public static final String NAME = "name";
public static final String TEAM_COLOR = "teamColor";
public static final String LEVEL = "level";
@PrimaryKey @Index
private long id;
private String name;
private String teamColor;
private int level;
public long getId() { return id; }
public void setId(long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getTeamColor() { return teamColor; }
public void setTeamColor(String teamColor) { this.teamColor = teamColor; }
public int getLevel() { return level; }
public void setLevel(int level) { this.level = level; }
}
To insert data I do it in the following way:
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
User entry = realm.createObject(User.class);
entry.setId(1); // aquí se indica el indice
entry.setName("user1");
entry.setTeamColor("red");
entry.setLevel(23);
}
});