我在Firebase中读取价值时遇到一些问题,当我运行项目时,收入和费用不会改变,好像它没有在Firebase上获得价值,但我不知道为什么?
private void getMoney()
{
myRef.child("money").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.child("income").exists()) {
income = Integer.parseInt(dataSnapshot.child("income").getValue().toString());
}
if (dataSnapshot.child("expense").exists()) {
expense = Integer.parseInt(dataSnapshot.child("expense").getValue().toString());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
money = income - expense;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtTong= (TextView) findViewById(R.id.txtTong);
getMoney();
txtTong.setText("Money: " + money);
initView();
答案 0 :(得分:2)
首先在(getMoney())的onDatachange方法中,更改此部分:
if (dataSnapshot.child("income").exists()) {
income = Integer.parseInt(dataSnapshot.child("income").getValue().toString());
}
if (dataSnapshot.child("expense").exists()) {
expense = Integer.parseInt(dataSnapshot.child("expense").getValue().toString());
}
到这个
if (dataSnapshot.child("income").exists()) {
income = dataSnapshot.child("income").getValue(Integer.class);
}
if (dataSnapshot.child("expense").exists()) {
expense = dataSnapshot.child("expense").getValue(Integer.class);
}
并更改此位置:
money = income - expense;
并将其放在onDatachange的末尾,在这些下面:
if (dataSnapshot.child("income").exists()) {
income = dataSnapshot.child("income").getValue(Integer.class);
}
if (dataSnapshot.child("expense").exists()) {
expense = dataSnapshot.child("expense").getValue(Integer.class);
}
//here you add it
money = income - expense;
修改强>
看看检索到的值没有问题,唯一的问题是在textview中显示值。
当你在onDatachange()之外设置TXT()时,文本视图不知道(money)的值是多少,因为只有当money放在onDatachange中时才会有正确的值。
所以与金钱有关的一切都应该包括在onDatachange
中所以你这样做:
myRef.child("money").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.child("income").exists()) {
income = dataSnapshot.child("income").getValue(Integer.class);
}
if (dataSnapshot.child("expense").exists()) {
expense = dataSnapshot.child("expense").getValue(Integer.class);
}
//here you set money
money = income - expense;
//and also here you set the text
txtTong.setText("Money: " + money);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
发生这种情况是因为在textview中设置money值之前无法保证调用onDatachange
答案 1 :(得分:0)
Can you make some changes in your code to
if (dataSnapshot.hasChild("income")) {
income = Integer.parseInt(dataSnapshot.child("income").getValue());
}
if (dataSnapshot.hasChild("expense")) {
expense = Integer.parseInt(dataSnapshot.child("expense").getValue());
}
而不是
if (dataSnapshot.child("income").exists()) {
income = Integer.parseInt(dataSnapshot.child("income").getValue().toString());
}
if (dataSnapshot.child("expense").exists()) {
expense = Integer.parseInt(dataSnapshot.child("expense").getValue().toString());
}
我认为你的情况没有成真尝试上面的代码。