How can I retrieve a random value from Firebase database?

后端 未结 1 492
一生所求
一生所求 2020-12-04 00:56

I have a firebase database with a tree that looks like this

tvthemetunes
airwolf value \"url to air wolftheme\"
eastenders value \"url to eastenders\"
knigh         


        
相关标签:
1条回答
  • 2020-12-04 01:39

    To solve this, please use the following lines of code:

    long childrenCount = snapshot.getChildrenCount();
    int count = (int) childrenCount;
    int randomNumber = new Random().nextInt(count);
    

    And then use a for loop to pull that value using the random number:

    int i=0;
    String themeTune; //Your random themeTune will be stored here
    for (DataSnapshot snap : snapshot.getChildren()) {
        if(i = randomNumber) {
            themeTune = snap.getValue(String.class);
            break;
        }
        i++;
    }
    plysound();
    
    0 讨论(0)
提交回复
热议问题