Home > Blockchain >  Error in reading the Firebase value: pricecode
Error in reading the Firebase value: pricecode

Time:05-14

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();
        }
    }

CodePudding user response:

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.

  • Related