Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class users extends RealmObject {
- @PrimaryKey
- private int id;
- private long icn;
- private String name;
- private String email;
- private String password;
- private int phone;
- public String getName() {
- return name;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public long getIcn() {
- return icn;
- }
- public void setIcn(long icn) {
- this.icn = icn;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public int getPhone() {
- return phone;
- }
- public void setPhone(int phone) {
- this.phone = phone;
- }
- realm.executeTransaction(new Realm.Transaction() { // must be in transaction for this to work
- @Override
- public void execute(Realm realm) {
- // increment index
- Number currentIdNum = realm.where(users.class).max(usersFields.ID);
- int nextId;
- if(currentIdNum == null) {
- nextId = 1;
- } else {
- nextId = currentIdNum.intValue() + 1;
- }
- users user = new users(); // unmanaged
- user.setId(nextId);
- //...
- realm.insertOrUpdate(user); // using insert API
- }
- }
- Realm realm = Realm.getDefaultInstance();
- // Realm transaction
- realm.executeTransactionAsync(new Realm.Transaction() {
- @Override
- public void execute(Realm bgRealm) {
- // Get the current max id in the users table
- Number maxId = bgRealm.where(users.class).max("id");
- // If there are no rows, currentId is null, so the next id must be 1
- // If currentId is not null, increment it by 1
- int nextId = (maxId == null) ? 1 : maxId.intValue() + 1;
- // User object created with the new Primary key
- users user = bgRealm.createObject(users.class, nextId);
- // Now you can update your object with your data. The object will be
- // automatically saved in the database when the execute method ends
- // ...
- // ...
- }
- }
- (int) System.currentTimeMillis() / 1000;
Add Comment
Please, Sign In to add comment