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

我正在做一个测验应用程序,在不同的章节中有很多问题。当然每一章都是一个数组。


但是现在,我已经到了需要一个特定章节来从所有章节中提取所有问题和答案的地步。所以基本上它是另一个数组中的一个数组。


 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());

它显示一个错误:


必需:Java.lang.String 找到:Java.lang.String[]


如何将问题和答案提取为字符串而不是数组。谢谢!


宝慕林4294392
浏览 165回答 1
1回答

慕桂英4014372

你打电话时questionBank.getChapterRandomTestQuestion(shuffledPositionChapterRandomTest[questionnumber])您正在检索String[],代表 中的条目之一chapterRandomTestQuestions。你需要使用questionBank.getChapterRandomTestQuestion(shuffledPositionChapterRandomTest[questionnumber])[someNumber]另外,我注意到您正在填充shuffledPositionChapterRandomTest整数的字符串表示。为什么不int[]改为使用它,并避免必须转换为 String 然后再转换回 int?另一个注释/问题:我认为您可能为数组编写了错误的内容。语法是SomeObject[]&nbsp;variable不是SomeObject&nbsp;variable[]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java