-1
realm1.beginTransaction();
                                        newuser tripnew = realm1.createObject(newuser.class);
 int nid= (int)(realm.where(newuser.class).max("nid").intValue()+1);
  tripnew.setNid(nid);

                                        tripnew.setFrom(frominput.getText().toString());
                                        tripnew.setTo(toinput.getText().toString());
                                        tripnew.setDatejourney(dateinput.getText().toString());
   realm1.commitTransaction();
     updatetrip();


    /// iam also use this code not working
    realm.where(newuser.class).maximumInt("id_cp") + 1;

newuser.java//////

public class newuser extends RealmObject {



   private  String from,to,datejourney;

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public String getDatejourney() {
        return datejourney;
    }

    public void setDatejourney(String datejourney) {
        this.datejourney = datejourney;
    }


}
Jas
  • 3,207
  • 2
  • 15
  • 45
  • Possible duplicate of [Realm and auto increment Behavior (Android)](http://stackoverflow.com/questions/30028282/realm-and-auto-increment-behavior-android) – SMR Aug 19 '16 at 06:22

1 Answers1

0

It looks that your class doesn't have nid nor id_cp numeric field. Add int nid; field along with accessors and then realm.where(newuser.class).max("nid").intValue()+1 should work in most of the cases. However it will fail if there is no newuser instance in database yet. I use singleton factory for generating primary keys as a more generic solution.

There is a long discussion in Realm Git Hub if you need more context: Document how to set an auto increment id?

zacheusz
  • 8,750
  • 3
  • 36
  • 60