从 Firebase 数据库接收数值数据

huangapple go评论69阅读模式
英文:

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(&quot;users&quot;).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(&quot;current_points&quot;).getValue(String.class);
            mTextViewPoints.setText(valuePoints);
        }

        @Override
        public void onCancelled(DatabaseError error) {
            // Failed to read value
            // Log.w(TAG, &quot;Failed to read value.&quot;, error.toException());
        }
    });
}

I hope this helps you

huangapple
  • 本文由 发表于 2020年9月3日 16:43:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63720002.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定