如何从Firebase数据库检索此数据

时间:2019-02-25 01:02:31

标签: java android firebase firebase-realtime-database

enter image description here

我想进入列表视图,查看“ Categorias”文件夹中的数据,但是我尝试了一切,但无法做到这一点。

片段代码:

myRef.addValueEventListener(new ValueEventListener() {

            public static final String TAG = "TNW";
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                // This method is called once with the initial value and again
                // whenever data at this location is updated.

                Map<String, Object> td = (HashMap<String,Object>) dataSnapshot.getValue();

                list3 values = td.values();


            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(getActivity().getApplicationContext(), "Ese usuario ya existe ", Toast.LENGTH_SHORT).show();
            }

        });

1 个答案:

答案 0 :(得分:0)

这非常简单,这是获取值的代码。

    DatabaseReference db = FirebaseDatabase.getInstance().getReference("people");

    db.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            for(DataSnapshot ds: dataSnapshot.getChildren()){

                //get the categorias node
                DataSnapshot dsCategorias = ds.child("categorias");

                //loop inside the categorias node for all children
                for(DataSnapshot dbValSnapshot: dsCategorias.getChildren()){

                    //Assuming all children have only boolean values

                    //getting the key and the values
                    String key = dbValSnapshot.getKey();
                    boolean value = dbValSnapshot.getValue(Boolean.class);
                }

            }

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

让我知道您是否无法理解我的代码的任何部分。谢谢