i am trying to add items to a realm database while manually implementing the auto increment id so it is easy for deleting items.
this is the add button which im trying to auto increment the id for the rows at
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
System.out.println(id);
realm.beginTransaction();
UserInfo userInfo=realm.createObject(UserInfo.class);
if (realm.where(UserInfo.class).endsWith("Id",userInfo.Id).findAll()!=null) {
id = Integer.parseInt(String.valueOf(realm.where(UserInfo.class).endsWith("Id", userInfo.Id).findAll())) + 1;
}
else{
id=0;
}
userInfo.setId(String.valueOf(id));
userInfo.setUsername(username.getText().toString());
userInfo.setPassword(password.getText().toString());
userInfo.setType(dropdown.getSelectedItem().toString());
realm.commitTransaction();
Toast.makeText(Add.this,"Username and Password Saved",Toast.LENGTH_LONG).show();
Intent toMain=new Intent (Add.this,List.class);
startActivity(toMain);
}
});
this is the UserInfo class which represents the realm database table
public class UserInfo extends RealmObject {
String Id;
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
String username;
String password;
String type;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
this is the error i get
java.lang.NumberFormatException: For input string: "[UserInfo = [{Id:null},{username:null},{password:null},{type:null}]]"