0

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}]]"

2 Answers2

0
if (realm.where(UserInfo.class).endsWith("Id",userInfo.Id).findAll()!=null) {
        if(userInfo.Id!=null)
        id = Integer.parseInt(String.valueOf(realm.where(UserInfo.class).endsWith("Id", userInfo.Id).findAll())) + 1;
        else
        id=0;
    }
else{
        id=0;
    }
MJM
  • 5,119
  • 5
  • 27
  • 53
  • i have just updated my entire question, i managed to make it delete from the recyclerview but not from the realm itself –  Jan 09 '18 at 12:01
  • then first get the list of UserInfo in List and pass the that list in recyclerview adapter – MJM Jan 09 '18 at 12:04
  • well my code works on the mobile i have, the emulater was getting kind of hinky, anyway now i gotta fix the problem where i did set the Id in realm as the current date, i need to implement it to be an autoincrement id –  Jan 09 '18 at 12:16
  • simple trick is that First way you can get count for number of object are stored then count++,it's your new id ,Second way you get last object added and get the id of that object +1,if there is no object start with 0 or 1 – MJM Jan 09 '18 at 12:20
  • i know that's the simplest way but somehow i can't manage to make it work, it's killing me –  Jan 09 '18 at 12:49
  • Update your question,how you are inserting new item,i will reply on that – MJM Jan 09 '18 at 13:15
  • Updated please check – MJM Jan 09 '18 at 14:00
  • same problem, i managed to find a solution for that anyway and now i'm back to having the swipe to delete as the one problem in the app lol –  Jan 09 '18 at 14:03
0

I think you should update your realm you are just setting the data to the pojo but not updating the realm after this

 userInfo.setType(dropdown.getSelectedItem().toString())
 realm.insertOrUpdate(userInfo);