获得随机值的Andr​​oid火力的java [英] get random value android firebase java

查看:128
本文介绍了获得随机值的Andr​​oid火力的java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过使用Java中的push(),以获得我的名单随机的。

下面是我的code来存储我的数据:

 火力地堡publRef = f.child(语言)子(德国)子(信息)。
                火力地堡newPublRef = publRef.push();
                地图<弦乐,对象>公共区域=新的HashMap<弦乐,对象>();
                publ.put(pubMsg,writeMsgField.getText()的toString());
                newPublRef.setValue集团(publ);

这是什么样子在我火力数据库:

 语言
   德语
      信息
        -Jf6ShYy7niHrqg_x4Tc:明天是风很大
        -Jf9v0xHAxINUUANrORU:今天是非常多风多雨

这是我如何找回我的数据:

 火力地堡F =新的火力地堡(https://myapp.firebaseio.com/Language/German/Message/);
    f.addValueEventListener(新ValueEventListener(){        公共无效onDataChange(DataSnapshot快照){
            disp_msg =(的TextView)findViewById(R.id.display_msg);
            。//disp_msg.setText(snapshot.getValue()的toString());
            可迭代< D​​ataSnapshot> DS = snapshot.getChildren();
            迭代器< D​​ataSnapshot> IDS = ds.iterator();
            地图<弦乐,对象> newPost =(地图<弦乐,对象>)。ids.next()的getValue();
            弦乐味精= newPost.get(pubMsg)的toString()。
            disp_msg.setText(msg.toString());        }

我想找回我的数据库的随机值。
例如。获取随机值时,它可以是明天是风很大或今天是非常多风多雨的。

你们能帮助我一些资料,我可以在java中使用,我还在其他语言的经验不足。谢谢你在前进。

解决

  //让孩子们的最大数量
        长allNum = snapshot.getChildrenCount();
        INT MAXNUM =(INT)allNum;        //儿童的数量得到随机整数
        INT randomNum =新的随机()nextInt(MAXNUM)。        诠释计数= 0;        //旁边会检查下一个值,而计数用作位置替代品。
        而(ids.hasNext()及和放大器; COUNT< randomNum){
            ids.next();
            算上++; //作为定位。
           }


解决方案

了解数据库中的可能值(numvals)的号码,然后使用在

 诠释随机=新的随机()nextInt(numvals)。

和选择日在数据库条目中的随机。

I want to get a random from my list by using push() in java.

Here is my code to store my data:

                Firebase publRef = f.child("Language").child("German").child("Message");
                Firebase newPublRef = publRef.push();
                Map<String, Object> publ = new HashMap<String, Object>();
                publ.put("pubMsg", writeMsgField.getText().toString());
                newPublRef.setValue(publ);

This is what it looks like in my firebase database:

Language
   German
      Message
        -Jf6ShYy7niHrqg_x4Tc: "Tomorrow is very windy"
        -Jf9v0xHAxINUUANrORU: "Today is very windy and rainy"

This is how I retrieve my data:

        Firebase f = new Firebase("https://myapp.firebaseio.com/Language/German/Message/");             
    f.addValueEventListener(new ValueEventListener() {

        public void onDataChange(DataSnapshot snapshot) {
            disp_msg = (TextView)findViewById(R.id.display_msg);
            //disp_msg.setText(snapshot.getValue().toString());
            Iterable<DataSnapshot> ds = snapshot.getChildren();
            Iterator<DataSnapshot> ids = ds.iterator();
            Map<String, Object> newPost = (Map<String, Object>) ids.next().getValue();
            String msg = newPost.get("pubMsg").toString();
            disp_msg.setText(msg.toString());

        }

I want to retrieve a random value in my database. For example. Get random value it can either be "Tomorrow is very windy" or "Today is very windy and rainy".

Can you guys help me with some information I can use in java, I'm still inexperienced in other languages. Thank you in advance.

SOLVED

        //getting maximum number of children
        long allNum = snapshot.getChildrenCount();
        int maxNum = (int)allNum;

        //getting the random integer from number of children
        int randomNum = new Random().nextInt(maxNum);

        int count = 0;

        //has next will check the next value while count is used as a position substitute.
        while(ids.hasNext() && count < randomNum) {
            ids.next();
            count ++; // used as positioning.
           }

解决方案

Find out the number of possible values (numvals) in the database and then use that in

int random = new Random().nextInt(numvals);

and select the "random"th entry in your database.

这篇关于获得随机值的Andr​​oid火力的java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆