0

I have an existing array that I created locally and import to Firebase and my array looks like this.

enter image description here

These both elements are objects created that have some many information related to appointments.

Now i am trying to create a new element with the same form, for example:

2-- |__ And the object I have created in my app

I have only managed or eliminate the rest of the elements (with setValue(object))

        Appointment newAppointment = new Appointment.Builder()
            .fechacita(dateSelected)
            .horacita(hourSelected)
            .usertoken(mAuthManager.getCurrentUserId())
            .oficina(centerSelected)
            .build();

    mDatabaseRef.child("LISTACITAS").setValue(newAppointment);

or create it with an ID that when recovering the data causes a crash in the application due to the deserialization of the objects that are not equal.

The Appointment object that I want to insert is

public class Appointment implements Parcelable {

private String fechacita;
private String horacita;
private Office oficina;
private String userID;
.....
}

The class is a normal Parcelable class that generates an object with her builder.

Please some help...

Sergio
  • 725
  • 1
  • 7
  • 20
  • 1
    Realtime Database can't automatically know what number is next in the array-like structure you've created. You should use push() to generate a new key, and use child properties to figure out how each child is ordered relative to each other. – Doug Stevenson Jun 11 '18 at 16:17
  • See my answer here: https://stackoverflow.com/questions/50787817/how-to-add-a-user-to-a-firebase-numbered-array/50788095#50788095 – Frank van Puffelen Jun 11 '18 at 16:37

1 Answers1

1

try this code

mDatabaseRef.push().setValue(incidentReportUser)

Write it this way (push() adds values instead of overriding).

Ans from here

UPDATE 1

if you want a series in key, not some random value, try this:

get the last key in the list using

Query dbQry = mDatabaseRef.orderByKey().limitToLast(1);
dbQry.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

       int key = Integer.parseInt(dataSnapshot.getKey());

       //Increment the key and add the object here using the earlier method
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
}

I have not checked this as of now, but you could get the idea

Darren Finch
  • 15
  • 1
  • 4
Arun Shankar
  • 2,603
  • 2
  • 26
  • 36
  • I tried this too but this solution generates a key that I don't want to generate. Possibly it cant be possible to do that but I am not sure. The way I want to do is as the same form as the image I post, without a key generated by Firebase and creating an object with the "key" 2, following the other objects – Sergio Jun 11 '18 at 15:35