Sergio
Sergio

Reputation: 798

How could I insert new data to array without key in Firebase

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...

Upvotes: 0

Views: 2074

Answers (1)

Arun Shankar
Arun Shankar

Reputation: 2593

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

Upvotes: 1

Related Questions