1

Please tell me what is my mistake? I'm trying to count the pricecode and shove it into user -> price. But instead, it gives an error or a link, and not the value "1000"

enter image description here

public void onClickB1 (View view)
    {
        DatabaseReference bd = FirebaseDatabase.getInstance().getReference("User");
        DatabaseReference bd1 = bd.child("pricecode");
        String id = mDataBase.getKey();
        //String key = dataSnapshot.getKey();
        String name = String.valueOf(textB1.getText());
        **String price = bd1.child("pricecode").getValue(String.class);**
        User newUser = new User(id,name,price);
        //mDataBase.push().setValue(newUser);

        if (!TextUtils.isEmpty(name)) // проверка пустой строки
        {
            mDataBase.push().setValue(newUser);
        }
        else
        {
            Toast.makeText(this,"Заполните поля",Toast.LENGTH_LONG).show();
        }
    }
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
ILGAMOV
  • 15
  • 3

1 Answers1

0

There is no way you can call getValue() on an object of the type DatabaseReference. Why? Because there is no such method inside the class. On the other hand, DataSnapshot class contains a getValue() method. So to be able to read that value, you have to attach a listener as in the following lines of code:

DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference userRef = db.child("User");
userRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            DataSnapshot snapshot = task.getResult();
            String priceCode = snapshot.child("pricecode").getValue(String.class);
            Log.d("TAG", "priceCode: " + priceCode);
        } else {
            Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
        }
    }
});

As I already mentioned in an earlier question of yours, store the prices as numbers and not strings.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Yes, I understand your remark. Thanks. But the event handler does not work in this form. The Price Code is not transferred to the User - price when the button is clicked. Example: public void onClickB1 (View view, Task task) { String id = mDataBase.getKey(); String name = String.valueOf(textB1.getText()); DataSnapshot snapshot = task.getResult(); String price = snapshot.child("pricecode").getValue(String.class); User newUser = new User(id,name,price); } – ILGAMOV May 13 '22 at 09:44
  • No, that is not how you should do it. Have you even tried the code in my answer? Does it print `1000`? – Alex Mamo May 13 '22 at 10:18
  • Yes, it works. But I wanted to implement it so that pricecode goes to user - price when the button is clicked. And in this case, I can't create an OnClick handler using your example, unfortunately. – ILGAMOV May 13 '22 at 10:30
  • If you need to have the `pricecode` under the `User` node, then you should use this code `db.child("User").child("-N1td...u0aB").child("pricecode").setValue("1000");` to add it. So don't forget to add the ID inside the reference, ok? – Alex Mamo May 13 '22 at 10:35
  • Thank you, I almost understood you. In my screenshot in the body of the question there is such a scheme: I want to manually write "pricecode" in the firebase database. But! When you click on the button, pass the value "pricecode" to "price". Now I'm doing it like this: String price = String.valueOf(db.child("User").child("pricecode")); and instead of the value "1000" it writes there: https://testkorzne-default-rtdb .firebaseio.com/User/pricecode this is my problem :( – ILGAMOV May 13 '22 at 10:53
  • I'm sorry but I cannot access that URL. In general, other questions that derive from the initial question, such as how to add data to the database, should be considered new questions and should be added separately. – Alex Mamo May 13 '22 at 10:58
  • So please post a new question, here on StackOverflow, using its own [MCVE](https://stackoverflow.com/help/mcve), so I and other Firebase developers can help you. – Alex Mamo May 13 '22 at 10:59
  • Thank you, I asked a new question! questions/72228681 – ILGAMOV May 13 '22 at 11:16
  • I'll take a look and if I'll know the answer, I'll write to you. – Alex Mamo May 13 '22 at 11:22