0

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: The database now

but here is what I want

the desired database

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.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
salRad
  • 320
  • 1
  • 8
  • 21

1 Answers1

3

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
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • Ok thank you but this is working only once, if I try to add another the app crashes – salRad Mar 15 '18 at 19:55
  • this is part of it ` Failed to inflate ColorStateList, leaving it to the framework java.lang.RuntimeException: Failed to resolve attribute at index 0 at android.content.res.TypedArray.getColor(TypedArray.java:401)` – salRad Mar 15 '18 at 20:00
  • thats a different problem, not related to the question: https://stackoverflow.com/questions/44692217/failed-to-inflate-colorstatelist-leaving-it-to-the-framework-java-lang-unsuppor – Peter Haddad Mar 15 '18 at 20:07
  • I think some kind of conflict is happening, only when database is empty the code is working. – salRad Mar 15 '18 at 20:07
  • attribute is related to xml: https://stackoverflow.com/questions/35956609/textinputlayout-runtimeexception-failed-to-resolve-attribute-at-index-24 – Peter Haddad Mar 15 '18 at 20:09
  • I know this is the only error I see, the problem is there is no pushid for some reason and the values are directly added to the client root – salRad Mar 15 '18 at 20:10
  • Thank you it worked after doing the following `String key = FirebaseDatabase.getInstance().getReference().child("client").push().getKey();` `ref.child(key).child("firstName").setValue(fname);` `ref.child(key).child("LastName").setValue(lname);` – salRad Mar 15 '18 at 20:52
  • OK i need another help..because I'm writing the data to firebase in two separate lines the onChildAdded reads only one part of the data when I first open the activity and I have to reopen it to get the other value on the screen...is there a way to solve this – salRad Mar 17 '18 at 22:56
  • please ask it as a question with more specific information, then me or someone else will check it and be able to answer you – Peter Haddad Mar 17 '18 at 22:57