How to retrive the push key value and update child values of key value

48
January 11, 2019, at 6:30 PM

Hello Everyone

I am using push method to add data on my realtime Firebase database. Now I would like to retrive sellername and update the new sellername from app and save changes in Firebase database as well.

How can I do that?

I'm using below code but I"m getting different key value each time randomly instead of -LVmsTmBZ_kCdcXlZh7e.

    FirebaseDatabase mFirebaseDatabase = FirebaseDatabase.getInstance();
   DatabaseReference ref =          mFirebaseDatabase.getReference().child("books").child("Classical Mechanics");
String key = ref.push().getKey();
          Toast.makeText(book_details.this, key, Toast.LENGTH_SHORT).show();

Then I am using:

                  FirebaseDatabase.getInstance().getReference()
.child("books")
.child(Classical Mechanics)
.child(key)
.child("sellername")
.setValue("newvalue");

And it gives me null error which is expected outcome since I'm getting random keyvalue instead of single constant.

Answer 1

I'm using below code but I"m getting different key value each time randomly instead of -LVmsTmBZ_kCdcXlZh7e.

This is happening because everytime you are using the push() method a brand new id is generated.

So if you want to access specific objects, you must know something that unique identifies those objects. In this case, if want you to change the value of your sellername property, you should use a query that looks like this:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference classicalMechanicsRef = rootRef.child("books").child("Classical Mechanics");
Query query = classicalMechanicsRef.orderByChild("sellername").equalTo("sagar");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            ds.child("sellername").getRef().setValue("newvalue");
        }
    }
    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
    }
};
query.addListenerForSingleValueEvent(valueEventListener);

Using this code, you'll change the name of the seller within all objects, which makes sense since the new name must exist within all objects.

Edit: It's even simpler to update/delete the data under a single object. For that please use the following lines of code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference classicalMechanicsRef = rootRef.child("books").child("Classical Mechanics").child("-LVmsTmBZ_kCdcXlZh7e");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        dataSnapshot.child("sellername").getRef().setValue("newvalue");
    }
    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
    }
};
classicalMechanicsRef.addListenerForSingleValueEvent(valueEventListener);
READ ALSO
CMake Error:Error occurred while communicating with CMake server

CMake Error:Error occurred while communicating with CMake server

When I new a CMake project by the Android Studio,it occures a problem when I open it by the first time:

61
How to prevent double items but also prevent entire list from refreshing?

How to prevent double items but also prevent entire list from refreshing?

I am getting a JSON Array of user ids strings from a database:

26
My app cannot see a library within a library

My app cannot see a library within a library

I created 2 Android librariesNames are LibA and LibB

59