4

Edit

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

Stations{
Uknradio
bbcradio1
bbcradio1extra
Beatles radio

then a StationsKey node that looks like this

Stations{
Uknradio = true
bbcradio1 =true
bbcradio1extra = true
Beatles radio = true

and so on... id like to pull a random station from the database

here is the code im working with which is the new answer given

    String DATABASE_CHILD = "STATIONS";
    String DATABASE_CHILD2 ="STATIONKEY";
    ref = db.getReference().child(DATABASE_CHILD2);
ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {

    for (DataSnapshot ds : snapshot.getChildren()) {
     String productId = ds.getKey();
      productIdsList.add(productId);
   }

  int productListSize = productIdsList.size();
    
ref = db.getReference().child(DATABASE_CHILD).child(productIdsList.get(new Random().nextInt(productListSize)));
ref2.addListenerForSingleValueEvent(new ValueEventListener() {
  @Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
    mUrl.clear();
    mTITLE.clear();
    mDOWN.clear();
    mIMAGE.clear();
    mUrl.clear();
    mUP.clear();
    mDESC.clear();

        for (DataSnapshot snap : snapshot.getChildren()) {

            station_items = snap.getValue(Stations_Firebase.class);
            String a  =station_items.getStation();
            String b = station_items.getTag();
            String c = station_items.getUrl();
            Long d = station_items.getUpvote();
            Long e = station_items.getDownvote();
            String f = station_items.getImage();

            mTITLE.add(a);
            mDESC.add(b);
            mUrl.add(c);
            mUP.add(String.valueOf(d));
            mDOWN.add(String.valueOf(e));
            mIMAGE.add(f);
        }

 private int nextInt(int productListSize) {
    return 0;
}

but now im getting this error

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.get(ArrayList.java:437)
    at com.p9p.radioify.ui.station_tabs.Random.getRandomStations(Random.java:103)
markharrop
  • 866
  • 1
  • 9
  • 30
  • 2
    If there's not many, do something more verbose and say that if a random integer is n, download the respective thing. Otherwise, get all of the urls in a list and select a random element in the list. Firebase isn't going to have any built-in function to get a random element from a section of a tree, if that's what you're looking for. – bearacuda13 Jan 14 '18 at 02:06
  • 1
    Please have a look at this [answer](https://stackoverflow.com/a/42043146/5861618) – Rosário Pereira Fernandes Jan 14 '18 at 08:58
  • 2
    Possible duplicate of [Firebase: Pull random data from Firebase to RecyclerView (android)](https://stackoverflow.com/questions/42042219/firebase-pull-random-data-from-firebase-to-recyclerview-android) – Rosário Pereira Fernandes Jan 14 '18 at 08:59

1 Answers1

7

Edit: 20th, August 2022

Maybe these other approaches will help:


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();
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • 1
    Have you checked the solution that exists in my updated answer? – Alex Mamo Aug 20 '22 at 07:19
  • 1
    I'll check this later. As soon as I've woke up properly ☺️ – markharrop Aug 20 '22 at 08:30
  • 1
    Give it a try and keep me posted. – Alex Mamo Aug 20 '22 at 08:34
  • the bit im not understanding is the nextInt(). ive made a public int called nextInt() but what should it return? – markharrop Aug 20 '22 at 15:56
  • You cannot return that. However, have tried one of the solutions that exist in the linked [answer](https://stackoverflow.com/questions/50413117/how-to-get-unique-random-product-in-node-firebase/50413208)? – Alex Mamo Aug 20 '22 at 17:01
  • 1
    It still uses the nextInt method. If I don't create a public int it just becomes an unresolved error. But I am currently changing the code to match the answer you have given – markharrop Aug 20 '22 at 17:41
  • Ok, give it a try and tell me if it works. – Alex Mamo Aug 20 '22 at 18:11
  • im getting this error pal java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.get(ArrayList.java:437) at com.p9p.radioify.ui.station_tabs.Random.getRandomStations(Random.java:103) any help you could give would be fantastic – markharrop Aug 21 '22 at 17:58