英文:
Receive numeric data from the Firebase Database
问题
以下是翻译好的内容:
我想在一个 TextView 中显示一个分数的数值,但是应用程序总是关闭。
这是我的代码:
FirebaseDatabase database = FirebaseDatabase.getInstance();
FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference myRef = database.getReference("users").child(currentFirebaseUser.getUid());
// 从数据库读取数据
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String valuePoints = dataSnapshot.child("current_points").getValue(String.class);
mTextViewPoints = findViewById(R.id.current_points);
mTextViewPoints.setText(valuePoints);
}
@Override
public void onCancelled(DatabaseError error) {
// 无法读取数值
// Log.w(TAG, "无法读取数值。", error.toException());
}
});
其他包含字母的数值都能正常显示,但只有纯数字时应用程序就关闭。我哪里做错了?
英文:
I want to display a numerical value for a score in a TextView, but the application always closes.
This is my code
FirebaseDatabase database = FirebaseDatabase.getInstance();
FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference myRef = database.getReference("users").child(currentFirebaseUser.getUid());
// Read from the database
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String valuePoints = dataSnapshot.child("current_points").getValue(String.class);
mTextViewPoints = findViewById(R.id.current_points);
mTextViewPoints.setText(valuePoints);
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
// Log.w(TAG, "Failed to read value.", error.toException());
}
});
}```
Other values that contain letters are numbers he displays, but when it comes only to numbers he closes.
Where am I going wrong?
</details>
# 答案1
**得分**: 1
亲爱的Vanderclin Rocha,
起初,在回调的ValueEventListener中不能定义TextView,你应该在视图启动时定义它。
你代码中的问题是textView返回了空值,你可以进行调试以查看它。
以下是获取数据并将其设置到TextView中的当前方法。
```java
FirebaseDatabase database = FirebaseDatabase.getInstance();
FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference myRef = database.getReference("users").child(currentFirebaseUser.getUid());
TextView mTextViewPoints = findViewById(R.id.current_points);
// 从数据库读取数据
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String valuePoints = dataSnapshot.child("current_points").getValue(String.class);
mTextViewPoints.setText(valuePoints);
}
@Override
public void onCancelled(DatabaseError error) {
// 无法读取数值
// Log.w(TAG, "Failed to read value.", error.toException());
}
});
希望这能对你有所帮助
英文:
Dear Vanderclin Rocha,
Initially, the TextView cannot be defined in callback ValueEventListener, you should be defined when the view gets started.
The issue in your code textView return null, you can do debugging and see it.
and this is the current way to get data and set it into TextView.
FirebaseDatabase database = FirebaseDatabase.getInstance();
FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference myRef = database.getReference("users").child(currentFirebaseUser.getUid());
TextView mTextViewPoints = findViewById(R.id.current_points);
// Read from the database
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String valuePoints = dataSnapshot.child("current_points").getValue(String.class);
mTextViewPoints.setText(valuePoints);
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
// Log.w(TAG, "Failed to read value.", error.toException());
}
});
}
I hope this helps you
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论