Firebase Database. Retrieve nested data

冷暖自知 提交于 2020-01-06 06:02:05

问题


MY CURRENT CODES (now its working)

@Override
protected void onStart() {
    super.onStart();

    petshopRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            petshopsList.clear();

            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                Petshops petshops = postSnapshot.getValue(Petshops.class);
                petshopsList.add(petshops);
            }

            CustomerPetshopAdapter adapter = new CustomerPetshopAdapter(CustomerViewPetshopActivity.this, R.layout.customlist_viewpetshop, petshopsList);
            lvPetshops.setAdapter(adapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

QUESTION:

Lets say i have

petshopRef = FirebaseDatabase.getInstance().getReference("Petshops"); 

How do I retrieve all data in Blue Circle (in my image above)


回答1:


You can retrieve all the data using the once value event. Then you can loop through it to get each key and value. I'm writing this logic in nodejs, you can convert it into your java logic. Example,

petshopRef.once('value', function(snap){   

snap.forEach(listData=>{     //list data will have top level auto generated keys

listData.forEach(element=>{
element.key // will be the key, in your case (brcat)
element.val() // will be the corresponding value to above key. in your case (3)

});
});

});


来源:https://stackoverflow.com/questions/50283866/firebase-database-retrieve-nested-data

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