我正在做一个测验应用程序,不同章节中有很多问题。当然每一章都是一个数组。
但是现在,我已经到了需要一个特定章节来提取所有章节中的所有问题和答案的地步。所以基本上它是另一个数组中的一个数组。
public void shuffleChapterRandomTest() {
shuffledPositionChapterRandomTest = new String[chapterRandomTestQuestions.length];
for (int k = 0; k < shuffledPositionChapterRandomTest.length; k++) {
shuffledPositionChapterRandomTest[k] = String.valueOf(k);
}
Collections.shuffle(Arrays.asList(shuffledPositionChapterRandomTest));
Log.i("TAG", "shuffle: " + shuffledPositionChapterRandomTest[0] + " " + shuffledPositionChapterRandomTest[1]);
}
public static String shuffledPositionChapterRandomTest[];
private String chapterRandomTestQuestions[][] = {
//This are all the chapters being called
Questions,
chapterTwoQuestions,
chapterThreeQuestions,
chapterFourQuestions,
chapterFiveQuestions,
chapterSixQuestions,
chapterSevenQuestions,
chapterEightQuestions,
chapterNineQuestions,
chapterTenQuestions,
chapterElevenQuestions,
chapterTwelveQuestions,
chapter13Questions,
chapter14Questions
};
//This is my get method
public String[] getChapterRandomTestQuestion(String a) {
return chapterRandomTestQuestions[Integer.parseInt(a)];
}
当我尝试从“问题”中提取字符串作为 TextView 的 setText 时。
private void updateQuestion() {
if (questionnumber < questionBank.getChapterRandomTestLength()) {
storeUserData.setInt(Constants.TOTAL_QUESTION,questionBank.getChapterRandomTestLength());
binding.tvCountQuestion.setText(questionnumber+1+"/"+questionBank.getChapterRandomTestLength());
binding.tvQuestion.setText(questionBank.getChapterRandomTestQuestion(shuffledPositionChapterRandomTest[questionnumber]));
binding.tvAnsOne.setText(questionBank.getChapterRandomTestChoice(shuffledPositionChapterRandomTest[questionnumber], 1));
binding.tvAnsTwo.setText(questionBank.getChapterRandomTestChoice(shuffledPositionChapterRandomTest[questionnumber], 2));
binding.tvAnsTnree.setText(questionBank.getChapterRandomTestChoice(shuffledPositionChapterRandomTest[questionnumber], 3));
answer = questionBank.getChapterRandomTestCorrectAnswer(shuffledPositionChapterRandomTest[questionnumber]);
questionnumber++;
显示错误:
Required: Java.lang.String Found: Java.lang.String[]
如何将问题和答案提取为字符串而不是数组。谢谢!
最佳答案
当你打电话时
questionBank.getChapterRandomTestQuestion(shuffledPositionChapterRandomTest[questionnumber])
您正在检索一个 String[]
,代表 chapterRandomTestQuestions
中的条目之一。您需要使用
questionBank.getChapterRandomTestQuestion(shuffledPositionChapterRandomTest[questionnumber])[someNumber]
此外,我注意到您正在使用整数的字符串表示形式填充shuffledPositionChapterRandomTest
。为什么不将其改为 int[]
,并避免必须先转换为 String,然后再转换回 int?
还有另一个注释/问题:我认为您可能为数组写了错误的内容。语法是
SomeObject[] variable
不是
SomeObject variable[]
关于java - 如何从字符串数组中获取字符串以便与 SetText 一起使用?安卓工作室,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52211585/