gpt4 book ai didi

java - 检索firebase实时数据库中节点下的所有数据

转载 作者:行者123 更新时间:2023-12-02 09:00:23 25 4
gpt4 key购买 nike

我有一个 Firebase 实时数据库,想使用该数据制作排行榜。我试图检索“分数”节点中的所有数据,但不确定如何循环以获取所有用户的分数。

数据库结构如下: enter image description here

我尝试使用的代码是这样的:

package com.example.securityapp;

import androidx.appcompat.app.AppCompatActivity;

import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;

import static java.lang.System.in;

public class leaderboard extends AppCompatActivity {

DatabaseReference databaseUsers;
FirebaseDatabase database = FirebaseDatabase.getInstance();
FirebaseAuth mAuth;
TextView score;
private static final String TAG = "leaderboard";



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_leaderboard);

databaseUsers = database.getReference().child("Scores");
mAuth = FirebaseAuth.getInstance();
FirebaseUser user = mAuth.getCurrentUser();
score = (TextView) findViewById(R.id.leaderboard_score);


databaseUsers.child("Scores").addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
System.out.println("The scores are " + dataSnapshot.child("Points").getValue().toString());
}
}


@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());

}


});

}
}

最佳答案

您对 JSON 的了解太深了,正在阅读 /Points/Points。由于该节点不存在,因此您会在 onDataChange 中获得一个空快照。

database.getReference().child("Scores").addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Log.i(TAG, "The scores are " + dataSnapshot.child("Points").getValue(Long.class));
}
}

@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
}
});

返回值的新代码:

public class leaderboard extends AppCompatActivity {

DatabaseReference databaseUsers;
FirebaseDatabase database = FirebaseDatabase.getInstance();
FirebaseAuth mAuth;
TextView score;
private static final String TAG = "leaderboard";



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_leaderboard);

mAuth = FirebaseAuth.getInstance();
FirebaseUser user = mAuth.getCurrentUser();
score = (TextView) findViewById(R.id.tableText);


database.getReference().child("Scores").addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
System.out.println("The score is: " + snapshot.toString());
score.setText(snapshot.toString());
}
}


@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());

}


});

}
}

这是我的进步:

enter image description here

关于java - 检索firebase实时数据库中节点下的所有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60216366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com