0

I have created an event handler for the onClick button. When I click on the button, I want to transfer a pre-recorded number in the database to the price section.

My problem is that: when I click on the button and want to pass the value "pricecode" recorded in the database to "price". But it is mandatory that the pricecode is pre-recorded in the database.

enter image description here

String price = String.valueOf(db.child("User").child("pricecode"));

and instead of the value "1000", it writes a reference to the key there. Read more in the screenshot.

public void onClickB1 (View view)

    {
        DatabaseReference db = FirebaseDatabase.getInstance().getReference();
        String id = mDataBase.getKey();
        String name = String.valueOf(textB1.getText());

        String price = String.valueOf(db.child("User").child("pricecode")); // PROBLEM

        User newUser = new User(id,name,price);
        //mDataBase.push().setValue(newUser);

        if (!TextUtils.isEmpty(name))
        {
            mDataBase.push().setValue(newUser);
        }
        else
        {
            Toast.makeText(this,"empty text",Toast.LENGTH_LONG).show();
        }
    }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
ILGAMOV
  • 15
  • 3

2 Answers2

0

String price = String.valueOf(db.child("User").child("pricecode")); and instead of the value "1000", it writes a reference to the key there.

That's the expected behavior since the following operation:

String price = String.valueOf(db.child("User").child("pricecode"))

Does not store in the price variable the actual value (1000) of the pricecode field. The code inside the valueOf() method is a reference, so when you're passing that reference to the valueOf() method, you get:

https://testkornze...

So there is no way you can read a value of a particular field that exists in the Realtime Database without attaching a listener. I answered earlier a question of yours on how to read the value of pricecode. So to be able to use the value of pricecode, all operations that need data from the database should be added inside the onComplete() method.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • "all operations that require data from the database must be added inside the onComplete() method." I don't quite understand what it looks like? Can I have a short sketch, please – ILGAMOV May 13 '22 at 12:14
  • How can onComplete be correctly combined with onCLick? – ILGAMOV May 13 '22 at 12:23
  • Perform the click only when the data is available. Try to take a look at this [answer](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method/47853774) too. – Alex Mamo May 13 '22 at 12:58
  • I'm sorry, but I don't understand how to link onClick and onComplete. I'm already too confused. – ILGAMOV May 13 '22 at 13:20
  • These are the steps. Attach on click listener, inside onClick, add the code that exists [here](https://stackoverflow.com/questions/72224450/error-in-reading-the-firebase-value-pricecode), and inside onComplete, use the value of `pricecode` tp create a new object, right? – Alex Mamo May 13 '22 at 13:26
  • 1
    Alex, thank you for your help! I will try to get the desired result. – ILGAMOV May 13 '22 at 14:17
0

As Alex explained this code merely builds a reference to a path in the database, but doesn't actually read the value from that path:

db.child("User").child("pricecode")

To read the value, you either need to call addListenerForSingleValueEvent , or call get on the reference, as shown int he Firebase documentation on reading data once. Based on that:

db.child("User").child("pricecode").get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (!task.isSuccessful()) {
            Log.e("firebase", "Error getting data", task.getException());
        }
        else {
            DataSnapshot snapshot = task.getResult()l

            String price = String.valueOf(snapshot.getValue());

            User newUser = new User(id,name,price);
            //mDataBase.push().setValue(newUser);

            if (!TextUtils.isEmpty(name))
            {
                mDataBase.push().setValue(newUser);
            }
            else
            {
                Toast.makeText(this,"empty text",Toast.LENGTH_LONG).show();
            }
        }
    }
});
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thank you! Now I tried your method, got "null". – ILGAMOV May 13 '22 at 14:17
  • What does "got null" mean there? --- Also: did you already step through the `onComplete` code in a debugger line by line, checking the value of each variable on each line? If so, what was the first line that didn't do what you expect it to do? – Frank van Puffelen May 13 '22 at 14:37
  • String price = String.valueOf(snapshot.getValue()); Yes, there is a record in firebase and outputs a null value Example: public void onClickB1 (View view) { *here is your code*} – ILGAMOV May 13 '22 at 14:41
  • I expect that the value of pricecode when clicking on the button (OnClick) will pass its value to price (user-price) – ILGAMOV May 13 '22 at 14:43
  • Null returns in user-price with this code (your fragment above) – ILGAMOV May 13 '22 at 14:46
  • What does `snapshot.getValue()` (so without the `String.valueOf`) output? – Frank van Puffelen May 13 '22 at 15:02
  • If String price = snapshot.getValue(); Error: Incompatible types. Found: 'java.lang.Object', required: 'java.lang.String' If String price = String.valueOf(snapshot.getValue()); Output: user - price = null Although pricecode = 1000 or any value – ILGAMOV May 13 '22 at 15:10
  • Yeah, that's not what I meant. I mean if you just `Log.d(snapshot.getValue())`, what do you get in the output. – Frank van Puffelen May 13 '22 at 15:32
  • Error: Incompatible types. Found: 'java.lang.Object', required: 'java.lang.String' I don't understand you, I'm sorry – ILGAMOV May 13 '22 at 19:00
  • If you `Log.d(snapshot.getValue())`, what does it output? – Frank van Puffelen May 14 '22 at 00:17