java - 从 android 中的 Activity 发送字符串数组

标签 java android

我正在开发一个测验应用程序,问题、答案和选项通过计时器嵌入到应用程序中。我还有另外两项 Activity :一项用于向用户显示他当前的分数,另一项用于在超时后查看已回答的问题。 我打算将用户在计时器结束之前尝试过的问题和答案发送到我的审核 Activity 中以供审核。 例如,由于我的应用程序中有 10 个问题,并且用户能够在时间过去之前尝试其中的 5 个问题,因此我需要将这 5 个问题放入我的审核 Activity 中进行审核。

QuestionLibrary.Java

// This file contains questions from QuestionBank
class QuestionLibrary {
    // array of questions
    private String mQuestions [] = {
          // Question goes here
   };
    // array of multiple choices for each question
    private String mChoices [][] = {
           // question choices goes here
    };

    // array of correct answers - in the same order as array of questions
private String mCorrectAnswers[] = {
         // answers appear here
};


    // method returns number of questions
    int getLength(){
        return mQuestions.length;
    }

    // method returns question from array textQuestions[] based on array index
    String getQuestion(int a) {
        return mQuestions[a];
    }

    // method returns a single multiple choice item for question-based on array index,
    // based on number of multiple choice item in the list - 1, 2, 3 or 4 as an argument
    String getChoice(int index, int num) {
        return mChoices[index][num-1];
    }

    //  method returns correct answer for the question based on array index
    String getCorrectAnswer(int a) {
        return mCorrectAnswers[a];
    }

}

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private QuestionLibrary mQuestionLibrary = new QuestionLibrary();
    Button button1;
    Button button2;
    Button button3;
    Button button4;
    TextView msingleQuestion;
    TextView mQuestion; //current question to answer
    TextView timer;
   MainActivity.CounterClass count_timer;   //countdown timer class
    private int mQuestionNumber = 0; // current question number
    private String mAnswer;  // correct answer for question in mQuestionView
    private int mquizNumber = 1;
    private long resume_timer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // set textViews here
        // Setting countdown timer
        timer = (TextView) findViewById(R.id.timer);
        timer.setText("00:5:00");
        // start timer here
        count_timer = new MainActivity.CounterClass(300000, 1000);
        count_timer.start();
        updateQuizNumber(mquizNumber);

    }

    private void updateQuizNumber(int mquizNumber) {
        msingleQuestion.setText("" + mquizNumber+"/"+mQuestionLibrary.getLength());
    }


    private void updateQuestion() {
        // check if we are not outside array bounds for questions
        if(mQuestionNumber<mQuestionLibrary.getLength() ){
            // set text for hint and click_me
            mClickMe.setText(mQuestionLibrary.getTextHint(mQuestionNumber));
            // set the text for new question, and new 4 alternative to answer on four buttons
            mQuestion.setText(mQuestionLibrary.getQuestion(mQuestionNumber));
            button1.setText(mQuestionLibrary.getChoice(mQuestionNumber, 1));
            button2.setText(mQuestionLibrary.getChoice(mQuestionNumber, 2));
            button3.setText(mQuestionLibrary.getChoice(mQuestionNumber, 3));
            button4.setText(mQuestionLibrary.getChoice(mQuestionNumber,4));
            mAnswer = mQuestionLibrary.getCorrectAnswer(mQuestionNumber);
            mQuestionNumber++;
        }
        else {
            Toast.makeText(BeginnerActivity.this, "That was the last question!", Toast.LENGTH_SHORT).show();
       startActivity(intent);
        }
    }

    public void onClick(View view) {
        //all logic for all answers buttons in one method

        if (mQuestionNumber < mQuestionLibrary.getLength()) {
            // once user answer the question, we move on to the next one, if any
                    updateQuestion();
                    mquizNumber++;
                    updateQuizNumber(mquizNumber);
                }

            }, 1000);


        } else {
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Intent intent = new Intent(MainActivity.this, ResultsActivity.class);
                    finish();
                    count_timer.cancel();
                }

            }, 1000);

        }

    @Override
    protected void onRestart() {
        //resume timer when user comes back to the app
        count_timer = new BeginnerActivity.CounterClass(resume_timer, 1000);
        count_timer.start();
        super.onRestart();
    }
}

如何将已尝试的问题从我的主要 Activity 转移到另一个 Activity 中以进行正确的审核?

最佳答案

您可以使用下面的代码通过 Intent 发送字符串数组

Bundle b=new Bundle();
b.putStringArray(key, stringArrayReference);
Intent i=new Intent(context, Class);
i.putExtras(b);

为了在其他 Activity 中获取字符串数组,您可以使用以下代码

Bundle b=this.getIntent().getExtras();
String[] array=b.getStringArray(key);

希望有帮助

关于java - 从 android 中的 Activity 发送字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47647889/

相关文章:

java - 如何在对 Spring Boot Controller 的 GET 请求中接受 LocalDateTime 参数?

android - "java.lang.IllegalStateException: not connected to MediaScannerService"在Android中添加文件到媒体库时

android - LocationListener 从未调用过

Android - 移除微调器下拉箭头

android - 运行牛轧糖的华为手机出现安全异常

java - 使用唯一约束的一对一映射未按预期工作

java - Tomcat/Linux 上的 UTF-8 和 Servlet

java - 赫罗库。我必须在 ProcFile 中输入什么?

java - FlowJob 的 Spring Batch 内存泄漏,重复步骤

java - 线程 "main"java.lang.RuntimeException : Stub XmlPullParserFactory 中的异常