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

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

显示错误:

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/

相关文章:

java - Google App Engine - 使用 Objectify 在本地连接到远程数据存储

android - 没有收到有关 PagedList 更新的通知

正在释放的 JAVA 指针未分配

java - 如何在 ionic 3 应用程序启动屏幕上设置自定义微调器

java - 使用 Java 8 流通过 boolean 函数将对象列表分组为列表列表

android - 计算与作为 2D 线段的倾斜或倾斜墙碰撞后移动球的角度

java - 对 RecyclerView 的更改导致此错误 :E/RecyclerView: No adapter attached; skipping layout

java - 在 Mac 上设置 Tomcat 服务器

swift - 更改 NSStatusBar 字体颜色和大小

java - 在 Android 中子类化 Activity 时,是否有一种设计模式可以减少代码重复?