passing list on Insert command using Room duplicating the values in Android -


i passing countrylist of country save in database using room. saving duplicating values , onconflict replace strategy not working.

appdatabase.getappdatabase(getapplicationcontext()).countrydao().insertalllist(countrylist); 

list values being duplicated though passing below replace strategy.

@insert(onconflict = onconflictstrategy.replace) 

here countrydao

@dao public interface countrydao {  @query("select * country") list<country> getallcountries();  @insert(onconflict = onconflictstrategy.replace) void insertalllist(list<country> countries); 

}

country object:

 @entity(tablename = "country")     public class country {     public int getid() {         return id;     }      public void setid(int id) {         this.id = id;     }      @primarykey(autogenerate = true)     private int id;     private long countryid;     private string countryname;      public long getcountryid() {         return countryid;     }      public void setcountryid(long countryid) {         this.countryid = countryid;     }      public string getcountryname() {         return countryname;     }      public void setcountryname(string countryname) {         this.countryname = countryname;     } } 

you must have different id each country , assign manually, otherwise each element have id=0 , override another.

try without autogenerate key

@primarykey(autogenerate = false) private int id; 

Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -