如何从字符串数组中获取字符串,以便可以与SetText一起使用? Android Studio

时间:2018-09-06 20:18:02

标签: java android macos android-studio

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

但是,现在,我到达了一个特定的章节,要从所有章节中提取所有问题和答案。因此,基本上它是另一个数组中的一个数组。

 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++;

显示错误:

  

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

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

1 个答案:

答案 0 :(得分:0)

致电时

df.columns.foldLeft( df )( (accDF, c) =>
  accDF.withColumn(c, TrimText(col(c)))
)

您正在检索一个questionBank.getChapterRandomTestQuestion(shuffledPositionChapterRandomTest[questionnumber]) ,代表String[]中的一项。您需要使用

chapterRandomTestQuestions

另外,我注意到您正在用整数的String表示填充questionBank.getChapterRandomTestQuestion(shuffledPositionChapterRandomTest[questionnumber])[someNumber] 。为什么不将其设置为shuffledPositionChapterRandomTest,而又不必转换为String然后又返回int?

还有另一个注意事项/问题:我想您可能为数组写了错误的东西。语法是

int[]

不是

SomeObject[] variable