I am trying to access the values of a particular key from the Firebase database.I want to update those values and update it

[This is my json dataThis is my code ][1]

I am trying to change the value of the key-value pair in the Firebase database.
Lets suppose I have a key value like this:”chemistry”:”20″.I want to extract the value of the key chemistry and want to update it after adding some number like I add 10 to it so my final key value pair will become “chemistry”:”30″ .But I am unable to access the key can someone help me with this.

My code goes here:

 mAuth.signInWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            // Sign in success, update UI with the signed-in user's information
                            Log.d("TAG", "signInWithEmail:success");
                            //extracting the current user from the databse
                            FirebaseUser user = mAuth.getCurrentUser();
                            //getting the uid of the user
                            String Uid=user.getUid();
                            //getting the databse instance for the particular user
                            FirebaseDatabase database = FirebaseDatabase.getInstance();
                            //setting the path to the Users/Uid to read the key values
                            DatabaseReference myRef = database.getReference("Users/"+Uid);


                            //textView.setText(myRef);
                            String key=myRef.child("chemistry").getKey();
                            textView.setText(key);

                           // myRef.setValue("Hello, World!");
                        } else {
                            // If sign in fails, display a message to the user.
                            Log.w("TAG", "signInWithEmail:failure", task.getException());
                            Toast.makeText(TestActivity.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                            //updateUI(null);
                        }

                        // ...
                    }
                });

my json is here:

{
  "Users" : {
    "LMn5GeZixNQNpdQhGzdKNbTgIw53" : {
      "chemistry" : "20",
      "email" : "umairnsr87@gmail.com",
      "lastScore" : "0",
      "maths" : "0",
      "name" : "umair",
      "phone" : "123456789",
      "physics" : "0"
    }
  }
}

2
Leave a Reply

avatar
2 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

In general, you should add some attempt code of what you are trying to achieve, so that others can better guide you to fix your problems. People is not supposed to write code for you.

Anyway, if your collection is called users. then you would retrieve a specific ID in this way:

db.collection("users").doc(THEID).get()
    .then( data => {
       console.log(data)
    })

use .add(jsondata) and .set(jsondata) for ading and updating

here’s the documentation:
https://firebase.google.com/docs/firestore/?gclid=CjwKCAjwm4rqBRBUEiwAwaWjjK7_CnO37twhvxrw2-WUxep6ykpTEaJiGnmojoBz74TNylodfkL7DxoCE9gQAvD_BwE

Jason
Guest

I am trying to change the value of the key-value pair in the firebase database. In general when you want to update a value of a property, you should use DatabaseReference’s updateChildren(Map update): Update the specific child keys to the specified values. Regarding the second issue: Lets suppose I have a key value like this:”chemistry”:”20″.I want to extract the value of the key chemistry and want to update it after adding some number like I add 10 to it so my final key value pair will become “chemistry”:”30″ Above, I showed you how you can simply update the value of… Read more »