Lee Royld
Lee Royld

Reputation: 75

Update object's field in Firebase

My project has 2 Apps: Admin-app and Client-app. First, I need to use the Admin-app in order to put data into Firebase and here is my data class

public class AllData {
    private String placeName;
    private ArrayList<String> category;
    private String address;
    private String openingHour;
    private String website;
    private HashMap<String, String> review; }

and I use following code to set data into Firebase

AllData alldata = new AllData(placeName, category, address,
                    , openingHour, website, null);
mDatabaseReference = mFirebaseDatabase.getReference().child("Store Data");
                        DatabaseReference storeData = mDatabaseReference.child(placeName);
                        storeData.setValue(alldata);

I have set the review field as null because I want to let my users review each place on Client-app and sync it into Firebase to the review field as HashMap<UserName, Review>

I use these codes in client-app to push review to Firebase

HashMap<String, String> reviewMap = new HashMap<>();
            reviewMap.put(UserName, review);
            mDatabase = FirebaseDatabase.getInstance();
            mReference = mDatabase.getReference().child("Store Data").child(selectedPlace.placeName).child("review");
            mReference.push().setValue(reviewMap);

This is not a right approach but that's all I could. What I want is to update the user's review to the AllData's review field in the Firebase asynchronously. How can I make this happen? Every answer is appreciated!

Upvotes: 1

Views: 752

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

You can simply use setValue() method directly on the reference without using a HashMap like this:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.child("Store Data").child(selectedPlace.placeName).child("review").setValue(yourValue);

In which yourValue is the value which you want to set to the review key.

Hope it helps.

Upvotes: 1

Related Questions