'Firebase read and onClick handler Error in reading the Firebase value: pricecode
Please tell me specifically how to fix the function so that when the button is clicked (onclick handler) passes the value from the database to the firebase USER - "price" branch. I can't figure it out for about two days. I will be grateful for a concrete example and correction of my function. Thanks. enter image description here
public void onClickB1 (View view)
{
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);
String id = db.getKey();
String name = "s";
String price = String.valueOf(snapshot.getValue());
User newUser = new User(id,name,price);
db.push().setValue(newUser);
} else {
Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
}
}
});
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|