为了账号安全,请及时绑定邮箱和手机立即绑定

如何从字符串数组中获取字符串以便它可以与 SetText 一起使用?安卓工作室

如何从字符串数组中获取字符串以便它可以与 SetText 一起使用?安卓工作室

宝慕林4294392 2021-09-03 15:14:14
我正在做一个测验应用程序,在不同的章节中有很多问题。当然每一章都是一个数组。但是现在,我已经到了需要一个特定章节来从所有章节中提取所有问题和答案的地步。所以基本上它是另一个数组中的一个数组。 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 methodpublic 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());它显示一个错误:必需:Java.lang.String 找到:Java.lang.String[]如何将问题和答案提取为字符串而不是数组。谢谢!
查看完整描述

1 回答

?
慕桂英4014372

TA贡献1871条经验 获得超13个赞

你打电话时

questionBank.getChapterRandomTestQuestion(shuffledPositionChapterRandomTest[questionnumber])

您正在检索String[],代表 中的条目之一chapterRandomTestQuestions。你需要使用

questionBank.getChapterRandomTestQuestion(shuffledPositionChapterRandomTest[questionnumber])[someNumber]

另外,我注意到您正在填充shuffledPositionChapterRandomTest整数的字符串表示。为什么不int[]改为使用它,并避免必须转换为 String 然后再转换回 int?

另一个注释/问题:我认为您可能为数组编写了错误的内容。语法是

SomeObject[] variable

不是

SomeObject variable[]


查看完整回答
反对 回复 2021-09-03
  • 1 回答
  • 0 关注
  • 164 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信