Reputation: 320
I'm trying to write data to firebase it is really amazing and makes everything seems like magic in a way but I'm having difficulties understanding the way it deals with data and arranging it, I've read the documentation and I still can't succeed in getting what I want (or maybe just feeling lost). I have a Client java class that has two parameters for now that I want to write firstName and lastName
package com.example.android.bookkeepingapp;
import com.google.firebase.database.Exclude;
import java.util.HashMap;
import java.util.Map;
public class Client {
//client information
private static long clientId = 0;
private String firstName;
private String lastName;
private String companyName;
private String address;
private String email;
private String phoneNumber;
public Client()
{}
public Client(String firstName, String mLastName)
{
//take the last value and increase it by one
clientId = clientId + 1;
this.firstName = firstName;
this.lastName = getmLastName();
}
public Client(String companyName)
{
this.companyName = companyName;
}
public String getmAddress() {
return this.address;
}
public String getmCompanyName() {
return this.companyName;
}
public String getmEmail() {
return this.email;
}
public String getmFirstName() {
return this.firstName;
}
public String getmLastName() {
return this.lastName;
}
public static long getClientId() {
return clientId;
}
public String getmPhoneNumber() {
return this.phoneNumber;
}
public void setmAddress(String address) {
this.address = address;
}
public void setmCompanyName(String companyName) {
this.companyName = companyName;
}
public void setmEmail(String email) {
this.email = email;
}
public void setmFirstName(String firstName) {
this.firstName = firstName;
}
public void setmLastName(String lastName) {
this.lastName = lastName;
}
public void setmPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
/**
* Map will use for writing the items to the database.
* @return
*/
@Exclude
public Map<String, Object> toMap() {
HashMap<String, Object> result = new HashMap<>();
result.put("firstName", firstName);
result.put("lastName", lastName);
/*result.put("companyName" ,companyName);
result.put("address" ,address);
result.put("email" ,email);
result.put("phoneNumber" ,phoneNumber);*/
return result;
}
}
What I want to achieve is to have a branch in the main database called client
mClientDatabaseReference = mFirebaseDatabase.getReference().child("client");
and here is what happens when new user is added
// create new client at /track-my-business/client/
final String key =FirebaseDatabase.getInstance().getReference().push().getKey();
// get user input and set it to result
// edit text
Client client = new Client(firstNameEditText.getText().toString(),
lastNameEditText.getText().toString());
Map<String, Object> clientValues = client.toMap();
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put(key, clientValues);
FirebaseDatabase.getInstance().getReference().updateChildren(childUpdates);
OK in short, this is what the data looks like after executing the above code:
but here is what I want
I want both the client firstName and lastName to be written inside the same key value, when I'm executing my code only the firsName is written successfully.
Upvotes: 0
Views: 1789
Reputation: 80914
updateChildren()
is used when you want to update a certain field.
To send the data you can simply do this:
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("client").push();
ref.child("firstName").setValue(fname);
ref.child("LastName").setValue(lname);
then you will have this:
client
pushid
firstName: fname
LastName:lname
pushid
//data of other client
Upvotes: 3