ArrayList随机挑选

时间:2017-02-23 15:39:57

标签: java android android-studio arraylist

每个人,我只是一个初学者,所以请耐心等待;) 我有一个ArrayList,它被随机挑选到我的cardView,如果我想按顺序一个接一个地选择它,我应该改变什么?

 public static class Questions {
        private String mQuestion;
        private String mAnswer;


        // statyczne tablice, na podstawie których zostaną uzupełnione obiekty artykułów
        private static String[] sQuestions = {"1 Lorem ipsum dolor sit amet",
                "2 Etiam sit", "3 Cras vel lorem",
                "Cras suscipit, urna at aliquam rhoncus",
                "Phasellus congue lacus eget neque",
                "Phasellus pharetra nulla ac diam"};


        private static String[] sAnswers = {"1 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin nibh augue, suscipit a, scelerisque sed, lacinia in, mi. Cras vel lorem. Etiam pellentesque aliquet tellus. Phasellus pharetra nulla ac diam.",
                "2 Quisque semper justo at risus. Donec venenatis, turpis vel hendrerit interdum, dui ligula ultricies purus, sed posuere libero dui id orci",
                "3 Nam congue, pede vitae dapibus aliquet, elit magna vulputate arcu, vel tempus metus leo non est. Etiam sit amet lectus quis est congue mollis.",
                "Phasellus congue lacus eget neque. Phasellus ornare, ante vitae consectetuer consequat, purus sapien ultricies dolor, et mollis pede metus eget nisi.",
                "Praesent sodales velit quis augue. Cras suscipit, urna at aliquam rhoncus, urna quam viverra nisi, in interdum massa nibh nec erat."};

        public Questions() {
            Random arry = new Random();

            mQuestion = sQuestions[arry.nextInt(sQuestions.length)];
            mAnswer = sAnswers[arry.nextInt(sAnswers.length)];
        }

        public String getTitle() {

            return mQuestion;
        }

        public String getContent() {
            return mAnswer;
        }
    }

5 个答案:

答案 0 :(得分:1)

需要更改这些行以指向所需的索引。

 mQuestion = sQuestions[arry.nextInt(sQuestions.length)];
 mAnswer = sAnswers[arry.nextInt(sAnswers.length)];

例如,如果您想在5处提出问题和答案,那么您需要编写

mQuestion = sQuestions[4];
mAnswer = sAnswers[4];

如何获得数字(在我的示例中为4)是您的选择。带增量的for循环或while循环将起作用。从键盘输入传递直接数字也可以。

答案 1 :(得分:1)

您的数组会随机返回,因为您已经以这种方式编写了它。 首先,看看你的代码

        Random arry = new Random();
        mQuestion = sQuestions[arry.nextInt(sQuestions.length)];
        mAnswer = sAnswers[arry.nextInt(sAnswers.length)];

由于随机函数,您正在使用数组的随机元素。 你需要做的是用你想要的元素的索引替换索引部分,即这个[arry.nextInt(sQuestions.length)]。如果你想要第一个 - > 0,2, - > 1等等。不要超过arr.length-1或抛出异常。

总而言之,例如,将你想要的项目的索引放在这里。:

    mQuestion = sQuestions[0];
    mAnswer = sAnswers[0];

吸气者会给你“1 Lorem ipsum dolor sit amet”和“1 Lorem ipsum dolor sit amet,consectetur adipiscing elit.Proin nibh augue,suscipit a,scelerisque sed,lacinia in,mi.Cras vel lorem.Etiam pellentesque aliquet tellus.Chasellus pharetra nulla ac diam。“

创建计数器,为调用函数时如何返回元素创建自己的逻辑。

答案 2 :(得分:0)

这是你的意思吗?

int a=0;
while (a<arry.length)
{
        mQuestion = sQuestions[a];
        mAnswer = sAnswers[a];
   a++;
}

答案 3 :(得分:0)

要迭代数组,您可以执行以下操作:

for (int i = 0; i < array.length; i++) {
    String s = array[i];
    // Do what you want with your string s
}

答案 4 :(得分:0)

为什么不添加全局变量作为数组的最大值?然后你可以在每次调用问题时迭代它。

if(questionPlace < max){
 //assign values, questionPlace++;
}else{
 questionPlace = 0;
 //assign values
}