简体   繁体   English

Firebase 如何在按键下检索数据

[英]Firebase How To Retrieve The Data Under A Push Key

I can't figure out how to retrieve my data without variable under the push key, so please help.我无法弄清楚如何在按键下没有变量的情况下检索我的数据,所以请帮忙。

    reference = FirebaseDatabase.getInstance().getReference("FireAlarm");
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot keySnapshot : dataSnapshot.getChildren()){
                dataBaru = keySnapshot.getKey();
                data.setText(dataBaru);
            }
        }

数据库结构

When you are using the following line of code:当您使用以下代码行时:

dataBaru = keySnapshot.getKey();

You are getting only those pushed keys.你只得到那些按下的键。 If you want to get the values corresponding to the keys, please use the following line of code:如果要获取键对应的值,请使用以下代码行:

dataBaru = keySnapshot.getValue(String.class);

One more thing, when looping through a DataSnapshot object using getChildren() method, you're most likely getting more than one result so you can simply set it to a TextView .还有一件事,当使用getChildren()方法循环遍历DataSnapshot对象时,您很可能会得到多个结果,因此您可以简单地将其设置为TextView So probably you should use a ListView for that or even better a RecyclerView .因此,您可能应该为此使用ListView甚至更好的RecyclerView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM