简体   繁体   中英

How can I retrieve a random value from Firebase database?

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

tvthemetunes
airwolf value "url to air wolftheme"
eastenders value "url to eastenders"
knight rider value "url to nightrider"

and so on...

in my app users download items and while items download a tv theme will play from the url. i can get it to work ok when value event for a single item. i want it to pick a value at random from the list. how can achieve this?

Edit can use recycle view method as my app does not contain any

this is my code for single item

protected Dialog onCreateDialog(int id) {

    switch (id) {
        case progress_bar_type:
    //Here is where i play the theme
            getthemetune();
            pDialog = new ProgressDialog(c);
            pDialog.setTitle(MY Title);
            pDialog.setMessage(MY Message);
            pDialog.setIcon(R.mipmap.ic_launcher);
            pDialog.setIndeterminate(false);
            pDialog.setMax(100);
            pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            pDialog.setCancelable(false);
            pDialog.show();

            return pDialog;
        default:

            return null;
    }
}
private void getthemetune(){
  mthemetuneref = FirebaseDatabase.getInstance();
    DatabaseReference ref = mthemetuneref.getReference().child(MYREF);
    ref.child("airwolf").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
//Edit to add Alex's answer?
          long childrenCount = datasnapshot.getChildrenCount();
          int count = (int) childrenCount;

//Dont know how to use this
          int randomNumber = new Random().nextInt(count);

            plysound();

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}
private void plysound(){
    mp = new MediaPlayer();
    String j =
    tvThemeTune.toString();
    Log.i("Url",j);
    try {
        mp.setDataSource(j);
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        mp.prepare();
    } catch (IOException e) {
        e.printStackTrace();
    }
    mp.start();
    Log.i("Sound playing", "Ok");
    mp.setLooping(true);
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM