How to read data from Firebase ONCE using java/android?

后端 未结 1 1483
谎友^
谎友^ 2020-12-08 20:36

I\'m trying to use the Java API to read data from a Firebase database in an Android application in the onCreate() event. In other words, I\'m trying to do the simplest read

相关标签:
1条回答
  • 2020-12-08 20:52

    That is the right method, and you're on the right track. The naming is just a little confusing (sorry!). If you do the addListenerForSingleValueEvent, your overridden onDataChange method will be called exactly once, with a DataSnapshot, just as you wish (and just like "ref.once('value' ...)" would do).

    So you should be able to do:

    // Add all polls in ref as rows
    polls.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            for (DataSnapshot child : snapshot.getChildren()) {
                ...
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题