读取 Firebase 值时出错:pricecode
Error in reading the Firebase value: pricecode
请告诉我我的错误是什么?我正在尝试计算 pricecode
并将其推入用户 -> 价格。但相反,它给出错误或 link,而不是值“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();
}
}
无法对 DatabaseReference. Why? Because there is no such method inside the class. On the other hand, DataSnapshot class contains a getValue() 方法类型的对象调用 getValue()
。因此,为了能够读取该值,您必须附加一个侦听器,如以下代码行所示:
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!
}
}
});
正如我在之前的 问题中提到的那样,将价格存储为数字而不是 字符串。
请告诉我我的错误是什么?我正在尝试计算 pricecode
并将其推入用户 -> 价格。但相反,它给出错误或 link,而不是值“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();
}
}
无法对 DatabaseReference. Why? Because there is no such method inside the class. On the other hand, DataSnapshot class contains a getValue() 方法类型的对象调用 getValue()
。因此,为了能够读取该值,您必须附加一个侦听器,如以下代码行所示:
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!
}
}
});
正如我在之前的