0

So far i have managed to display the contents of a row(4 columns) in specific textviews. But i want to display them in random. I use this

String answer1 = cur.getString(cur.getColumnIndex("ANSWER1"));

and on click the content change but always the data on column ANSWER1 shows in the first textview. What can i do to random display the 4 columns to the 4 textviews? E.g column 1 to textview 3, column 2 to textview 1 etc.I want everytime different data on textview 1.
----Edit----
Lets say i have one more column that idicates the number of the column the correct answer is.E.g if the correct answer is in the column "Answer3" the data in the column "CorrAnswer" is 3. I change the textviews above to buttons. Each button sends an integer. How can i check the correct answer when the texts on the buttons are shuffled?

4

1 回答 1

1

我会将您的所有答案添加到 ArrayList,然后使用 Random 删除随机答案。以下 Java(控制台)代码显示了您可以如何执行此操作:

    ArrayList<String> lstAnswers = new ArrayList<String>();
    lstAnswers.add("Answer 1");
    lstAnswers.add("Answer 2");
    lstAnswers.add("Answer 3");
    lstAnswers.add("Answer 4");

    Random random = new Random();


    while (lstAnswers.size() > 0) {
        int index = random.nextInt(lstAnswers.size());
        String randomAnswer = lstAnswers.remove(index);
        System.out.println(randomAnswer);
    }

根据您的编辑要求,以下内容会将文本分配给文本视图:

    ArrayList<String> lstAnswers = new ArrayList<String>();
    lstAnswers.add("Answer 1");
    lstAnswers.add("Answer 2");
    lstAnswers.add("Answer 3");
    lstAnswers.add("Answer 4");

    Random random = new Random();

    int[] textViews = new int[] { R.id.txt1, R.id.txt2, R.id.txt3, R.id.txt4 };
    int textViewIndex = 0;

    while (lstAnswers.size() > 0) {
        int index = random.nextInt(lstAnswers.size());
        String randomAnswer = lstAnswers.remove(index);

        ((TextView)findViewByid(textViews[textViewIndex])).setText(randomAnswer);

        ++textViewIndex;
    }
于 2012-04-09T09:47:02.810 回答