简体   繁体   English

如何从Firebase数据库检索随机值?

[英]How can I retrieve a random value from Firebase database?

I have a firebase database with a tree that looks like this 我有一个带有这样的树的firebase数据库

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. 在我的应用程序中,用户下载项目,而项目下载时,电视主题将从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: 然后使用for循环使用随机数提取该值:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Firebase 实时数据库中检索值? - How can I retrieve the value from Firebase Realtime database? 使用选项从Firebase数据库检索随机数据 - Retrieve random data from firebase database with options 如何尝试在RecyclerView中检索值我尝试了很多,但是在Android中将数据库中的数据推送到Firebase后,它从Firebase的ID中检索了空值 - How to Retrieve Value in RecyclerView I tried a lot but it Retrieve a Null Value from Generated ID by Firebase after Push Data in Database in Android 如何从 Firebase 数据库中获取特定值(数据) - How can I fetch particular value(data) from Firebase Database 如何从 firebase 数据库 android 检索单选按钮的值 - how to retrieve value of a radio button back from firebase database android 如何根据用户在文本字段中输入的电子邮件从 Firebase 数据库中检索值? - How can I retrieve values from Firebase database based on email entered by user in text field? 如果包含列表,如何从 Firebase 实时数据库中检索对象? - How can I retrieve an object from the Firebase RealTime Database if it contains a List? 如何从 Firebase 实时数据库中检索用户用户名以获取欢迎回复消息? - How can I retrieve a users username from Firebase Realtime database for a welcome back message? 如何从Firebase数据库检索数据以显示标记? - How can I retrieve data from my Firebase Database to display markers? 如何在两个不同的活动中从Firebase实时数据库检索数据 - How can I retrieve data from Firebase Realtime Database in two different activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM