java - I want to add object to realm DB every time an item is clicked from the recyclerview -


i followed tutorial it's not working well, here's did:

public class realmhelper {     realm realm;     realmresults<fav_places> fav_places;     boolean saved;      public realmhelper(realm realm)     {         this.realm = realm;      }       public boolean save(final fav_places favs) {         if (favs == null) {             saved = false;          } else {              realm.executetransaction(new realm.transaction() {                 @override                 public void execute(realm realm) {                     try {                          fav_places fav = realm.copytorealm(favs);                         saved =true;                     }                     catch (realmexception e){                         e.printstacktrace();                         saved=false;                     }                 }              });         }         return saved;     }        public list<com.nuku.mc.populate_recyclerview.fav_places> retreivefrom() {         fav_places = realm.where(fav_places.class).findall();         return null;     }      public arraylist<fav_places> justrefresh() {         arraylist<fav_places> latest = new arraylist<>();         (fav_places f : fav_places) {             latest.add(f);         }         return latest;     }  } 

here pojo:

public class fav_places extends realmobject {      @primarykey     int fav_id;      public string place_name;     public string image;     public string place_category;     public string description;      public fav_places(){      }      public fav_places(string name, string image) {         this.place_name = name;         this.image = image;      }      public string getplace_name() {         return place_name;     }      public void setplace_name(string name) {         this.place_name = name;     }      public void setplace_image(string image){         this.image = image;     }      public string getplace_image() {         return image;     } 

}

here code transaction:

case r.id.target: {                             bucket.setimageresource(r.drawable.ic_target_icon_clicked);                              int position = getadapterposition();                             places p = places_list.get(position);                              string name = p.getplace_name();                             string image = p.getplace_image();                              fav_places f = new fav_places(name,image);                              realmconfiguration config = new realmconfiguration.builder(context).build();                             realm = realm.getinstance(config);                              realmhelper helper = new realmhelper(realm);                             helper.save(f);                          }                          break; 

am not setting id(thought auto increament) here getting

 fatal exception: main                                                    io.realm.exceptions.realmprimarykeyconstraintexception: value exists: 0                                                        @ io.realm.internal.table.throwduplicateprimarykeyexception(table.java:721)                                                        @ io.realm.internal.table.addemptyrowwithprimarykey(table.java:473)                                                        @ io.realm.realm.createobject(realm.java:708)                                                        @ io.realm.fav_placesrealmproxy.copy(fav_placesrealmproxy.java:397)                                                        @ io.realm.fav_placesrealmproxy.copyorupdate(fav_placesrealmproxy.java:387)                                                        @ io.realm.defaultrealmmodulemediator.copyorupdate(defaultrealmmodulemediator.java:99)                                                        @ io.realm.realm.copyorupdate(realm.java:1272)                                                        @ io.realm.realm.copytorealm(realm.java:727)                                                        @ com.nuku.mc.populate_recyclerview.realmhelper$1.execute(realmhelper.java:36)                                                        @ io.realm.realm.executetransaction(realm.java:1065)                                                        @ com.nuku.mc.populate_recyclerview.realmhelper.save(realmhelper.java:31)                                                        @ com.nuku.mc.populate_recyclerview.placesadapter$viewholder.onclick(placesadapter.java:159)                                                        @ android.view.view.performclick(view.java:4212)                                                        @ android.view.view$performclick.run(view.java:17476)                                                        @ android.os.handler.handlecallback(handler.java:800)                                                        @ android.os.handler.dispatchmessage(handler.java:100)                                                        @ android.os.looper.loop(looper.java:194)                                                        @ android.app.activitythread.main(activitythread.java:5371) 

=> question how set primary key this? should static count++?


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -