且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Firebase Value事件侦听器

更新时间:2021-11-05 21:54:03

Egek92的答案是正确的.而且我想我知道为什么它不提供您想要的数据.也许使用以下代码,您将:

Egek92's answer is right. And I think I know why it doesn't provide data you want. Maybe with this following code, you will:

DatabaseReference reference = database.getReference("Stats").child("Matematik");
reference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        // dataSnapshot value will be Matematik, {ahmetozrahat=50, nihatkeklik=50}
        // because it is indeed the value we need

        // But you want key value pair to be added to your stats
        // So we can just loop through the values

        for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
            stats.put(childSnapshot.getKey(), childSnapshot.getValue().toString());
        }
        StatsAdapter adapter = new StatsAdapter(stats);
        listView.setAdapter(adapter);
    }
    ...
}

希望这会有所帮助