0
我有一個ArrayList>我從PHP 這裏存儲我的提問者它看起來像{{question,optA,optB,optC,optD,answer},{question,optA,optB,optC,optD,answer} ,.....}}Android ArrayList Shuffle
我該如何洗牌只有我的A,B,C,D像D,B,A,C? {問題,optD,optB,OPTA,OPTC,回答}使用
林這段代碼
//myArray
ArrayList<ArrayList<String>> getQuestion = new ArrayList<ArrayList<String>>();
ArrayList<String> choices;
//first retrive elements from getQuestionArray
questIndex = getQuestion.get(arrayRow).get(0);
aIndex = getQuestion.get(arrayRow).get(1);
bIndex = getQuestion.get(arrayRow).get(2);
cIndex = getQuestion.get(arrayRow).get(3);
dIndex = getQuestion.get(arrayRow).get(4);
answerIndex = getQuestion.get(arrayRow).get(5);
//get the correct answer (This is Word of senteces)
String correctAnswer = "";
if(answerIndex.equals("a")){
correctAnswer = aIndex;
}else if(answerIndex.equals("b")){
correctAnswer = bIndex;
}else if(answerIndex.equals("c")){
correctAnswer = cIndex;
}else if(answerIndex.equals("d")){
correctAnswer = dIndex;
}
//Im using this to shuffle my abcd Im creating new ArrayList for that
choices.add(aIndex);
choices.add(bIndex);
choices.add(cIndex);
choices.add(dIndex);
Collections.shuffle(choices);
int correctIndex;
//Find now where is our correct answer
correctIndex = choices.indexOf(correctAnswer);
//our correct letter now
switch(correctIndex){
case 0:
answerIndex = "a";
break;
case 1:
answerIndex = "b";
break;
case 2:
answerIndex = "c";
break;
case 3:
answerIndex = "d";
break;
}
aIndex = choices.get(0);
bIndex = choices.get(1);
cIndex = choices.get(2);
dIndex = choices.get(3);
question.setText(questIndex);
cA.setText(aIndex);
cB.setText(bIndex);
cC.setText(cIndex);
cD.setText(dIndex);
有沒有辦法做到這一點?
你Should't初始化你選擇的ArrayList? – joao2fast4u 2014-11-05 15:22:09
你的實現有什麼問題? – joao2fast4u 2014-11-05 15:38:08