Firebase无法检索数据

时间:2016-11-19 17:05:48

标签: android firebase firebase-realtime-database

模特课程:

@IgnoreExtraProperties
public class Points {

public String score;

public Points(){

}

public Points(String score) {
    this.score = score;
}

public String getScore() {
    return score;
}

public void setScore(String score) {
    this.score = score;
    }
}

更新代码:

  private void updateUser(String name) {
    FirebaseUser user = firebaseAuth.getCurrentUser();
    if (!name.equals("")) {
        dbRefer.child("User").child(user.getUid()).child("score").setValue(name);
        Toast.makeText(this, "update Done", Toast.LENGTH_SHORT).show();
    }
}

检索代码:

 private void addUserChangeListener() {
    final FirebaseUser getData = firebaseAuth.getCurrentUser();
    // User data change listener
    dbRefer.child("User").child(getData.getUid()).child("score").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Points user = dataSnapshot.getValue(Points.class);

            Toast.makeText(GameActivity.this, "Retrieved Data is: " + getData, Toast.LENGTH_SHORT).show();

            // Check for null
            if (user == null) {
                Log.e(TAG, "User data is null!");
                return;
            }

            Log.e(TAG, "User data is changed!" + user.getScore());

            // Display newly updated name and email
            mTvShowData.setText(user.getScore());

            // clear edit text
            mTxtEdtData.setText("");

            toggleButton();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(GameActivity.this, "Can't Get data", Toast.LENGTH_SHORT).show();
        }
    });
    }

更新代码正在运行,我现在面临的是检索部分,我似乎无法从我的数据库中获取数据。需要帮助和指导,谢谢。

1 个答案:

答案 0 :(得分:0)

通常情况下,我会像你一样提取信息,作为对象,但是因为你只提取一条信息,而不是创建一个点对象而且所有这些我只需手动拉动并放置它。

     dbRefer.child("User").child(getData.getUid()).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) { 
String name = (String) dataSnapshot.child("score").getValue();
mTvShowData.setText(name);