簡體   English   中英

嘗試隨機化Android應用程序中的幾個問題時出錯

[英]Error when trying to randomize few questions in android app

問候語,對此錯誤需要幫助。 嘗試谷歌搜索,但沒有解決問題。 我確實在發生錯誤的行中添加了備注。

注意下面的代碼有2個不同的Java類

[IMG =“ screenshot”] http://i.imgur.com/XYFHZOz.png[/IMG]

public class QuestionAndAnswer {
public List<String> allAnswers; // distractors plus real answer
public String answer;
public String question;
public String selectedAnswer;
public int selectedId = -1;

public QuestionAndAnswer(String question, String answer, List<String> distractors) {
    this.question = question;
    this.answer = answer;
    allAnswers = new ArrayList<String>(distractors);

    // Add real answer to false answers and shuffle them around
    allAnswers.add(answer);
    Collection.shuffle(allAnswers); //error on this line
}

public boolean isCorrect() {
    return answer.equals(selectedAnswer);
}

}

public class UjiSalah extends Activity {
RadioGroup rg;
int currentQuestion = 0;
TextView tv;
List<QuestionAndAnswer> quiz = new ArrayList<QuestionAndAnswer>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.uji_salah);

    tv = (TextView) findViewById(R.id.tvque);
    rg = (RadioGroup) findViewById(R.id.radioGroup1);

    // Setup a listener to save chosen answer
    rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if(checkedId > -1) {
                QuestionAndAnswer qna = quiz.get(currentQuestion);
                qna.selectedAnswer = ((RadioButton) group.findViewById(checkedId)).getText().toString();
                qna.selectedId = checkedId;
            }
        }
    });

    String[] question = {"Bilangan Rokaat Solah Zuhur ?","Bilangan Rokaat Solat Subuh ?","Bilangan Rokaat Solat Maghrib ?"};
            String[] answer = { "4 rokaat","2 rokaat","3 rokaat" };
            String[] distractor = { "5 rokaat","1 rokaat","4 rokaat","2 rokaat","3 rokaat","4 rokaat","4 rokaat","5 rokaat","3 rokaat" };

            ArrayList<String> distractorList = Arrays.asList(distractor); //error on this line


    int length = question.length;
    for(int i = 0; i < length; i++)
        quiz.add(new QuestionAndAnswer(question[i], answer[i], distractorList.subList(i * 3, (i + 1) * 3)));
    Collection.shuffle(quiz); //error here

    fillInQuestion();
    }

    public void fillInQuestion() {
        QuestionAndAnswer qna = quiz.get(currentQuestion);
        tv.setText(qna.question);

        // Set all of the answers in the RadioButtons
        int count = rg.getChildCount();
        for(int i = 0; i < count; i++)
            ((RadioButton) rg.getChildAt(i)).setText(qna.allAnswers.get(i));

        // Restore selected answer if exists otherwise clear previous question's choice
        if(qna.selectedId > -1)
            rg.check(qna.selectedId);
        else
            rg.clearCheck();
    }
}

+ 1 @ Prera​​k Sola,您可以將allAnswers聲明為ArrayList或將其強制轉換為:

Collection.shuffle((ArrayList)allAnswers);

List沒有實現Collection接口,但是ArrayList卻實現了:

https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html

列表測驗= new ArrayList <>();

ArrayList<QuestionAndAnswer> quiz = new ArrayList<>();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM