LizG
LizG

Reputation: 2530

Unable to update value in Firebase database from Android app

With the following code, I have the "history node".

History Node

 {
  "History" : {
    "-LJANnl9ofbXlxLGxTGg" : {
      "destination" : "3 Paradise Row",
      "driver" : "ReGqRl2IIUhmLIqdBqBIELHBsgE3",
      "location" : "1 Union St",
      "payment response" : "approved",
      "rating" : 0,
      "ridePrice" : 5.25,
      "rider" : "l3PPyBQux7YJhGGTdexxwDBteLM2",
      "riderPaid" : "true",
      "status" : "accepted",
      "timestamp" : 1533494377
    }
  }

With the following code, I am trying to update "status" value to update from "accepted" to "arrived_loc". When I run the code, I have a yellow highlight in firebase but keeps the value of status "accepted".

update status to "arrived_loc"

driverId = FirebaseAuth.getInstance().getCurrentUser().getUid();

    final DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    Query query = rootRefchild("History").orderByChild("driver").equalTo(driverId);
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            DataSnapshot nodeDS = dataSnapshot.getChildren().iterator().next();
            String key = nodeDS.getKey();
            Log.e(TAG, "key = " + key);
            String path = "/" + dataSnapshot.getKey() + "/" + key;
            Log.e(TAG, "path = " + path);

            HashMap<String, Object> update = new HashMap<>();
            update.put("status", "arrived_loc");
            rootRef.child(path).updateChildren(update);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

What am I doing wrong?

Edit #1

If I run this code:

DatabaseReference arrivedLoc = FirebaseDatabase.getInstance().getReference("History");
    String historyId = arrivedLoc.push().getKey();
    arrivedLoc.child(historyId).child("status").setValue("arrived at pickup");

I get an extra history node and no update:

"History" : {
    "-LJAVN2iAWfRREdlBevb" : {
      "destination" : "3 Union St",
      "driver" : "ReGqRl2IIUhmLIqdBqBIELHBsgE3",
      "location" : "123 Main Street",
      "payment response" : "approved",
      "rating" : 0,
      "ridePrice" : 5.38,
      "rider" : "l3PPyBQux7YJhGGTdexxwDBteLM2",
      "riderPaid" : "true",
      "status" : "accepted",
      "timestamp" : 1533496391
    },
    "-LJAVaFd3Gzq3iIZGHeP" : {
      "payment response" : "approved",
      "riderPaid" : "true",
      "status" : "accepted"
    }
  }

Edit #2 - image of database frozen

enter image description here

Edit #3

I can't seem to get it working for some reason so I renamed "status" to be "requestStatus" and created a new variable called rideStatus like:

status = "arrived at pickup";

    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference().child("History");
    rootRef.child(requestId).child("rideStatus").setValue(status);

This seems to work for now, but when I need to change the value of rideStatus, hopefully it will work.

Upvotes: 2

Views: 1200

Answers (2)

ʍѳђઽ૯ท
ʍѳђઽ૯ท

Reputation: 16976

Try with this code without push() function:

DatabaseReference arrivedLoc = FirebaseDatabase.getInstance().getReference("History");
    String historyId = arrivedLoc.getKey();
    arrivedLoc.child(historyId).child("status").setValue("arrived at pickup");

(depends on what you need to achieve on your app, might be a little different)


Update #2:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference().child("History");
            addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if(dataSnapshot.exists()){

                        for(DataSnapshot snapshot : dataSnapshot.getChildren()){
                            snapshot.getRef().child("status").setValue("arrived at pickup");
                        }
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }

Upvotes: 1

Mr.O
Mr.O

Reputation: 843

Try with updateChildren():

driverId = FirebaseAuth.getInstance().getCurrentUser().getUid();
HashMap<String, String> newData = new HashMap<>();
newData.put("status", "arrived_loc");
final DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference().child("History/" + driverId); // Or however the path is
rootRef.updateChildren(newData);

Upvotes: 0

Related Questions