Guest User

Untitled

a guest
Nov 1st, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. public class users extends RealmObject {
  2.  
  3. @PrimaryKey
  4. private int id;
  5. private long icn;
  6. private String name;
  7. private String email;
  8. private String password;
  9. private int phone;
  10.  
  11. public String getName() {
  12. return name;
  13. }
  14.  
  15. public int getId() {
  16. return id;
  17. }
  18.  
  19. public void setId(int id) {
  20. this.id = id;
  21. }
  22.  
  23. public long getIcn() {
  24. return icn;
  25. }
  26.  
  27. public void setIcn(long icn) {
  28. this.icn = icn;
  29. }
  30.  
  31. public void setName(String name) {
  32. this.name = name;
  33. }
  34.  
  35. public String getEmail() {
  36. return email;
  37. }
  38.  
  39. public void setEmail(String email) {
  40. this.email = email;
  41. }
  42.  
  43. public String getPassword() {
  44. return password;
  45. }
  46.  
  47. public void setPassword(String password) {
  48. this.password = password;
  49. }
  50.  
  51. public int getPhone() {
  52. return phone;
  53. }
  54.  
  55. public void setPhone(int phone) {
  56. this.phone = phone;
  57. }
  58.  
  59. realm.executeTransaction(new Realm.Transaction() { // must be in transaction for this to work
  60. @Override
  61. public void execute(Realm realm) {
  62. // increment index
  63. Number currentIdNum = realm.where(users.class).max(usersFields.ID);
  64. int nextId;
  65. if(currentIdNum == null) {
  66. nextId = 1;
  67. } else {
  68. nextId = currentIdNum.intValue() + 1;
  69. }
  70. users user = new users(); // unmanaged
  71. user.setId(nextId);
  72. //...
  73. realm.insertOrUpdate(user); // using insert API
  74. }
  75. }
  76.  
  77. Realm realm = Realm.getDefaultInstance();
  78. // Realm transaction
  79. realm.executeTransactionAsync(new Realm.Transaction() {
  80. @Override
  81. public void execute(Realm bgRealm) {
  82. // Get the current max id in the users table
  83. Number maxId = bgRealm.where(users.class).max("id");
  84. // If there are no rows, currentId is null, so the next id must be 1
  85. // If currentId is not null, increment it by 1
  86. int nextId = (maxId == null) ? 1 : maxId.intValue() + 1;
  87. // User object created with the new Primary key
  88. users user = bgRealm.createObject(users.class, nextId);
  89. // Now you can update your object with your data. The object will be
  90. // automatically saved in the database when the execute method ends
  91. // ...
  92. // ...
  93. }
  94. }
  95.  
  96. (int) System.currentTimeMillis() / 1000;
Add Comment
Please, Sign In to add comment