英文:
Displaying data from Real-time database on Firebase
问题
我正在尝试显示数据库中的数据,但在这样做时遇到了问题。我试图显示的数据被保存为"total",并且保存为整数类型。我已经尝试将其保存为字符串和双精度浮点数,但仍然没有成功。理想情况下,我想要显示总数,然后在总数超过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" 被保存在一个名为 "FAQ" 的节点中,该节点位于一个名为 "User" 的节点下。
数据结构如下:
这是一张存储在特定节点下的一些数据的截图
以下是我用来创建 "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);
}
英文:
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. 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.
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"
The data structure is as follows:
This is a screenshot some data stored under set nodes
The below data is the code I used to create the FAQ node.
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);
}
}
答案1
得分: 1
要获取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()); //不要忽略错误!
}
};
totalRef.addListenerForSingleValueEvent(valueEventListener);
在logcat中的结果将会是:
total: 15
我看到total
是一个数字,但是如果您将其保存为字符串,可以使用以下代码行:
String total = dataSnapshot.getValue(String.class);
英文:
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:
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:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论