Using textView arrays for questionnaire app

2019-07-27 07:12发布

I want to build a questionnaire type app with questions that show up individually and when a user submits an answer and clicks the button the data is stored in a textView above and the question goes to the next question where that answer is stored in a new textView. I am having difficulty getting the question to change to the next question.

Below I have attempted using the array method but have gotten errors both here

myTexts[0]=findViewById(R.id.question1);
myTexts[1]=findViewById(R.id.question2); 

-- and

 myTexts[questionNumber].setText(mEdit.getText().toString());
 questionNumber++;

Below is complete source code:

package com.example.greg;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class menu extends Activity {

Button   mButton;
EditText mEdit;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mButton = (Button)findViewById(R.id.button);
    mEdit   = (EditText)findViewById(R.id.userAnswereditText);

    TextView [] myTexts = new TextView[2];
    myTexts[0]=findViewById(R.id.question1);
    myTexts[1]=findViewById(R.id.question2);

    int questionNumber = 0;
    mButton.setOnClickListener(
    new View.OnClickListener()
       {
            public void onClick(View view)
            {

                myTexts[questionNumber].setText(mEdit.getText().toString());
                questionNumber++;   
            }
        });


}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
}

}

2条回答
萌系小妹纸
2楼-- · 2019-07-27 08:01

Shouldn't this...

myTexts[0]=findViewById(R.id.question1);

be a cast to TextView instead?...

myTexts[0]=(TextView)findViewById(R.id.question1);

That will definitely eliminate a ClassCastException

查看更多
够拽才男人
3楼-- · 2019-07-27 08:04

To change question you should have array of String or List of String that can hold all the question and as soon as button is clicked and the answer is stored in the TextView you will load the next question from that array or List of String that is holding the questions.

 String [] questions;
 int numberOfQuestions = 2;

and then in onCreate() method

 questions=new String[numberOfQuestions];//numberOfQuestion refers to integer that holds total number of questions
 questions[0]="This is first question?";
 questions[1]="This is second question?";

and now in your view.onClickListener

mButton.setOnClickListener(
new View.OnClickListener()
   {
        public void onClick(View view)
        {

            myTexts[questionNumber].setText(mEdit.getText().toString());
            questionNumber++;   
            //here you will have to load the next question 
            if(questionNumber < numberOfQuestions)
                questionTextViewHolder.setText(questions[questionNumber]);
            else
                Toast.makeText(menu.this,"No more questions!",Toast.LENGTHLONG).show();

            //Note one thing that your questionNumber variable should not increase myTexts length or questions length because if it does you will get  exception
        }
    });

questionTextViewHolder is the TextView in which you are displaying the question that is to be answered, you will have to replace "questionTextViewHolder " with your textview in which you are displaying the questions ! Class name menu starting letter should be capital if you follow the coding conventions.

查看更多
登录 后发表回答