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

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

问题描述



为了向用户显示不同的数据,每次都会显示第一个屏幕,其中包含从Firebase数据库中的一个节点随机挑选的数据。对于我的应用程序是非常重要的

有没有办法在原生Android中实现这一点,所有快照都是相同的模型

Collections.shuffle()

来做到这一点。

我做的是,把快照存储在一个数组列表里。

  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());






$ b

然后在这个数组列表中调用shuffle方法。 / p>

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

现在你可以用随机数组列表来做任何事情。


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

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

解决方案

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());
                        }
}

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天全站免登陆