[英]Displaying data from Real-time database on Firebase
I am trying to display data in my database but I'm having a problem doing so.我正在尝试在我的数据库中显示数据,但这样做时遇到了问题。 The data I'm trying to display is saved as "total" and it is saved as an int.
我试图显示的数据保存为“总计”,并保存为 int。 I have tried saving it as a string and double but still no luck.
我尝试将其保存为字符串和双精度,但仍然没有运气。 Ideally, I would like to display the total then display a message if the total is over the value 9. This is what I have so far.
理想情况下,我想显示总数,然后在总数超过 9 时显示一条消息。这就是我目前所拥有的。
public class Results extends AppCompatActivity {
private DatabaseReference mDatabase;
private FirebaseAuth fAuth;
private Button btn;
private TextView userR;
private TextView num;
private FirebaseUser currentUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_results);
userR = (TextView) findViewById(R.id.userResults);
btn = (Button) findViewById(R.id.generate);
fAuth = FirebaseAuth.getInstance();
currentUser = fAuth.getCurrentUser();
mDatabase = FirebaseDatabase.getInstance().getReference().child("FAQ");
}
@Override
public void onStart() {
super.onStart();
ValueEventListener scoreListener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
faqinputs FAQInputs = dataSnapshot.getValue(faqinputs.class);
userR.setText(FAQInputs.getTotal());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
};
mDatabase.addValueEventListener(scoreListener);
}
} }
"total" is saved in a "FAQ" node which is saved under a "User" “total”保存在“FAQ”节点中,该节点保存在“User”下
The data structure is as follows: This is a screenshot some data stored under set nodes数据结构如下:这是set节点下存储的一些数据的截图
The below data is the code I used to create the FAQ node.下面的数据是我用来创建FAQ节点的代码。
private void sendScoreData() {
String currentUserID = fAuth.getUid();
faqinputs FAQInputs = new faqinputs(questionOne, questionTwo, questionThree, questionFour, questionFive, questionSix, questionSeven, questionEight, questionNine, questionTen, total);
mDatabase.child("Users").child(currentUserID).push().child("FAQ").setValue(FAQInputs);
}
}
To get the value of the total
property, you have to add all node names in your reference, like in the following lines of code:要获取
total
属性的值,您必须在引用中添加所有节点名称,如以下代码行:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference totalRef = rootRef.child("Users").child(uid).child("-M4YRj8nbobkuKurbHqC").child("FAQ").child("total");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int total = dataSnapshot.getValue(Integer.class);
Log.d("TAG", "total: " + total);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage()); //Don't ignore errors!
}
};
totalRef.addListenerForSingleValueEvent(valueEventListener);
The result in the logcat will be: logcat 中的结果将是:
total: 15
I see the total to be a number, however, if you save it as a String, use this line:我看到总数是一个数字,但是,如果将其保存为字符串,请使用以下行:
String total = dataSnapshot.getValue(String.class);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.