Firebase - 随机查询 [英] Firebase - random query

查看:28
本文介绍了Firebase - 随机查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作和应用程序,显示第一个屏幕,其中包含一堆从 Firebase 数据库中的节点随机选取的数据.

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 time is kind of important for my application

无论如何在原生Android中实现这一点,所有快照都是相同的模型

Is there anyway to achieve this in native Android, all snapshots are of same model

推荐答案

firebase 数据库没有提供直接的方法,但你可以使用 Collections.shuffle()

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<Integer> array=new ArrayList<>();

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

然后在这个数组列表上调用shuffle方法.

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.

这篇关于Firebase - 随机查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆