0

enter image description hereHello 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.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
redface
  • 305
  • 1
  • 6
  • 18

1 Answers1

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);
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Alex Thanks it works ...one more question How to delete the whole key if sellername equals to sagar?? – redface Jan 11 '19 at 10:07
  • You're welcome @redface ! Simply using `ds.child("sellername").getRef().getParent().removeValue();`. Cheers! – Alex Mamo Jan 11 '19 at 10:10
  • I realized it is not solution to my problems, I have a list of data with each unique id and only want to change the sellername of someid not all which requires me to get get the key and change the value of sellername only inside that key not all.and also want to delete the same key not all. – redface Jan 11 '19 at 10:56
  • In this case, please see my answer from **[this](https://stackoverflow.com/questions/51787784/how-to-get-specific-pushedid-in-firebase/51788244)**. So in code should look like this: `rootRef.child("books").child("Classical Mechanics").child("LVmsTmBZ_kCdcXlZh7e").addListenerForSingleValueEvent(/* ... */)` without the need to loop, right? – Alex Mamo Jan 11 '19 at 11:01
  • what I should inside addListenerForSingleValueEvent(/* ... */); I am really sorry but I"m new to firebase and everything I learned is from google searches so no idea how to proceed further...I'm back to square one where using my posted code I'm getting random id each different tiem. – redface Jan 11 '19 at 11:07
  • Please see my updated answer. And hope you reconsider accepting my answer. – Alex Mamo Jan 11 '19 at 11:12