Skip to content Skip to sidebar Skip to footer

Using Textview Arrays For Questionnaire App

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

Solution 1:

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=newString[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.

Solution 2:

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

Post a Comment for "Using Textview Arrays For Questionnaire App"