Firebase - random query

后端 未结 3 663
情书的邮戳
情书的邮戳 2020-11-30 13:32

I am making and application that show a first screen with a bunch of randomly picked data from a node in Firebase database.

To present user with different data every

3条回答
  •  爱一瞬间的悲伤
    2020-11-30 14:16

    There is no direct way provided by firebase database but you can do this using Collections.shuffle()

    What i did was,Take the snapshot and store it in an arraylist.

     private ArrayList array=new ArrayList<>();
    
        public void onDataChange(DataSnapshot dataSnapshot) {
                            for (DataSnapshot imageSnapshot : dataSnapshot.getChildren()) {
                               MyClass myclass = imageSnapshot.getValue(MyClass.class);
                               array.add(myclass.someFunction());
                            }
    }
    

    Then call the shuffle method on this array list.

    Collections.shuffle(array); // randomize the arraylist
    

    Now you can do whatever you want with this randomized arraylist.

提交回复
热议问题