Retrieve all data under node in firebase realtime database

旧巷老猫 提交于 2020-02-16 08:40:08

问题


I have a firebase realtime database and want to use the data to make a leaderboard. Im trying to retrieve all the data in my Scores node but am not sure how to loop through to get the scores of all users.

The database structure is as below:

The code I have tried to use is this:

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());

    }


});

        }
}

回答1:


You're going one level too deep into your JSON and are reading /Points/Points. Since that node doesn't exist, you get an empty snapshot in your 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());
    }
});

New Code that returns values:

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());

    }


});

        }
}

This is my progress:




回答2:


It appears as though you are using a "Listener" class to access the data and print out the scores. The "ValueEventListener" listener you attached, with the "onDataChange()" method implemented, would not be called until there is a data change.

I'm not sure how you are using this class, but based on your question, I am assuming that you just want to print the scores when the "leaderboard.onCreate()" method is executed. Is this correct? If that is the case, you may not need to be attaching a listener.



来源:https://stackoverflow.com/questions/60216366/retrieve-all-data-under-node-in-firebase-realtime-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!