Firebase push data on same child using Hashmap

11,175

From what I understand, you want all the attributes under one node. Something like this

null
  |-------uniqueKey
           |------birthdate
           |------birthplace
           |------firstname
           |------lastname
           |------missingdate
           |------personAge
           |------personHeight
           |------personWeight

However, you are calling push. mDatabase.child("null").push().setValue(dataMap); What 'push' does is it literally pushes a new node on the database and and when you call setValue you add the dataMap to that new 'push key'. You should use push when working with lists of data. When you call push the first time it adds an new node to the 'null' child you made.

You get this

null
 |
-----FirstuniquePushKey

And in your second activity when you call push again, you get this

null
 |-------FirstUniqueyKey
 |         |--------
 |-------SecondUniqueKey
           |--------

If you want to add attributes to the same child(unique key). Traverse down to that node like this

   mDatabase.child("null").child("FirstUniqueKey").child("pesronAge").setValue('n/a')

This way you are not calling 'push' and therefore not creating a new 'push' key. You are at the firstuniqueKey node and you create another child called personAge and you set its value. I hope this makes sense.

Edit

You must be calling set value. If you are doing this,

mDatabase.child("null").child("FirstUniqueKey").setValue(HashMap). 

That is why it overwrites what you had you previously. You can achieve what you want in two ways

1) For each key you have in the second hashmap. Set the value.

    mDatabase.child("null").child("FirstUniqueKey").child("PersonAge").setValue('')

mDatabase.child("null").child("FirstUniqueKey").child("PersonHeight").setValue('')

 mDatabase.child("null").child("FirstUniqueKey").child("PersonWeight").setValue('')  

2)The second way to achieve it create a new pushKey.

mDatabase.child("null").child("FirstUniqueKey").push().setValue(HashMap)

Notice how I am at the child "FirstUniqueKey" and I push and then set the value.

Share:
11,175
DavidA
Author by

DavidA

Updated on June 08, 2022

Comments

  • DavidA
    DavidA about 2 years

    For my android application i'm trying to add multiple strings to the same child (Firebase Unique ID) in which the user will enter on different layouts. I'm using a Hashmap to put the strings into Firebase. I have 4-5 different layouts that the user would come across and needs to fill out the information, but when I save it in the Database, it always goes under a different Unique ID and I need it all to be under the same one.

    In the image I need all the information to be under just the first unique ID.

    Here's my code, I created a Parent class with every layout (the other classes) having some method to access the Hashmap.

    Parent Class:

    public class FireBaseDataMap {
    
        private HashMap<String, String> dataMap = new HashMap<String, String>();
    
        public HashMap<String, String> fireebaseMap(){
            return dataMap;
        }
    
    }
    

    First Activity: I put an arrow where I needed help in the onClick(View view) method.

        public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
        //UI References
        private EditText firstName;
        private EditText lastName;
        private EditText birthPlace;
        private EditText etxtBirthdate;
        private EditText etxtMissingDate;
        private Button saveButton;
        private Button cancel_button;
    
    
        private DatePickerDialog birthdateDatePickerDialog;
        private DatePickerDialog missingDatePickerDialog;
    
        private DatabaseReference mDatabase;
    
        private SimpleDateFormat dateFormatter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    
            dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
    
            findViewsById();
    
            setDateTimeField();
    
            saveButton = (Button) findViewById(R.id.saveButton);
            cancel_button = (Button) findViewById(R.id.cancel_button);
    
            saveButton.setOnClickListener(this);
            cancel_button.setOnClickListener(this);
    
        }
    
        private void findViewsById() {
    
            firstName = (EditText) findViewById(R.id.firstName);
            lastName = (EditText) findViewById(R.id.lastName);
            birthPlace = (EditText) findViewById(R.id.birthPlace);
    
    
            etxtBirthdate = (EditText) findViewById(R.id.etxt_birthdate);
            etxtBirthdate.setInputType(InputType.TYPE_NULL);
            etxtBirthdate.requestFocus();
    
    
            etxtMissingDate = (EditText) findViewById(R.id.etxt_missingdate);
            etxtMissingDate.setInputType(InputType.TYPE_NULL);
            etxtMissingDate.requestFocus();
    
    
        }
    
        @Override
        public void onClick(View view) {
            if(view == etxtBirthdate) {
                birthdateDatePickerDialog.show();
            }
    
            if(view == etxtMissingDate){
                missingDatePickerDialog.show();
            }
            if(view == saveButton){
    
                String FirstName = firstName.getText().toString().trim();
                String LastName = lastName.getText().toString().trim();
                String BirthPlace = birthPlace.getText().toString().trim();
                String BirthDate = etxtBirthdate.getText().toString().trim();
                String MissingDate = etxtMissingDate.getText().toString().trim();
    
                mDatabase = FirebaseDatabase.getInstance().getReference();
    
                // Here is where im accessing the parent class and need help <------
                FireBaseDataMap obj = new FireBaseDataMap();
                HashMap<String, String> dataMap = obj.fireebaseMap();
    
                if(FirstName.isEmpty()) {
                    FirstName = "n/a";
                    dataMap.put("firstName", FirstName);
                }
                else
                    dataMap.put("firstName", FirstName);
    
                if(LastName.isEmpty()) {
                    LastName = "n/a";
                    dataMap.put("lastName", LastName);
                }
    
                else
                    dataMap.put("lastName", LastName);
    
                if(BirthPlace.isEmpty()) {
                    BirthPlace = "n/a";
                    dataMap.put("birthPlace", BirthPlace);
                }
    
                else
                    dataMap.put("birthPlace", BirthPlace);
    
                if(BirthDate.isEmpty()) {
                    BirthDate = "n/a";
                    dataMap.put("birthDate", BirthDate);
                }
    
                else
                    dataMap.put("birthDate", BirthDate);
    
                if(MissingDate.isEmpty()) {
                    MissingDate = "n/a";
                    dataMap.put("missingDate", MissingDate);
                }
    
                else
                    dataMap.put("missingDate", MissingDate);
    
    
                mDatabase.child("null").push().setValue(dataMap);
    
            }
    
            if(view == cancel_button){
                startActivity(new Intent(this, PhysicalAttributes.class));
    
            }
        }
    
    }
    

    Second Activity: I'm just posting the method where I need help on for this activity so it wont seem so long, but if need the full class to understand then I can update it.

     public void onClick(View view) {
    
    
    
            if (view == saveButton) {
    
    
                String PersonAge = personAge.getText().toString().trim();
                String PersonHeight = personHeight.getText().toString().trim();
                String PersonWeight = personWeight.getText().toString().trim();
    
                mDatabase = FirebaseDatabase.getInstance().getReference();
    
                FireBaseDataMap obj = new FireBaseDataMap();
                HashMap<String, String> dataMap = obj.fireebaseMap();
    
    
                if(PersonAge.isEmpty()){
                    PersonAge = "n/a";
                    dataMap.put("personAge", PersonAge);
                }
    
                else
                    dataMap.put("personAge", PersonAge);
    
                if(PersonHeight.isEmpty()) {
                    PersonHeight = "n/a";
                    dataMap.put("personHeight", PersonHeight);
                }
                else
                    dataMap.put("personHeight", PersonHeight);
    
                if(PersonWeight.isEmpty()) {
                    PersonWeight = "n/a";
                    dataMap.put("personWeight", PersonWeight);
                }
                else
                    dataMap.put("personWeight", PersonWeight);
    
                mDatabase.child("null").push().setValue(dataMap);
    
    
            }
            if (view == cancel_button) {
    
    
            }
    
        }
    }
    

    I've tried looking all over and tried different ways to make it work, but nothing. Any help would be appreciated. If clarification is needed then will try my best.

    Another way I was thinking of is if I can access the same saveButton on every activity if that'll work.

  • Frank van Puffelen
    Frank van Puffelen over 7 years
    Please don't post pictures of ASCII art. Just post the ASCII art itself and indent it four spaces to make it show up as preformatted text.
  • Umar Karimabadi
    Umar Karimabadi over 7 years
    Yh, I know. It was not indenting so I just posted the picture instead. ; )
  • DavidA
    DavidA over 7 years
    I just tried this and it works, it does add it to the same node, but it removes the previous one that was already added. @UmarKarimabadi
  • DavidA
    DavidA over 7 years
    Any idea how I will go about fixing this? @UmarKarimabadi
  • Umar Karimabadi
    Umar Karimabadi over 7 years
    I am not 100% sure of what you are trying to achieve. If you can explain a bit more I think I can help ; )
  • DavidA
    DavidA over 7 years
    Pretty much exactly what you explained, but when I try your method out, the previous values in the Hashmap are deleted, so it just overwrites it. Firebase database is in real time, so I can see it getting added to the right place, then removing the old ones. When I put in all the string values from the first layout, it works fine. I go to the next layout to do the same thing it also works but just removes what was previously in there when I need both of them to stay. If that makes sense to you! @UmarKarimabadi
  • DavidA
    DavidA over 7 years
    Thank you sooo much! What you helped me with makes so much sense and it works exactly how I want it to. Thanks ! @UmarKarimabadi
  • Umar Karimabadi
    Umar Karimabadi over 7 years
    My pleasure. Happy coding!