开发者

Beginning Android Programming - Building a short Quiz

开发者 https://www.devze.com 2023-01-18 16:57 出处:网络
Okay, so I\'m writing my first Android app and am basically stuck. The app is a short 4 question quiz with two choices per question. This creates 16 possible results, which I\'ve created descriptions

Okay, so I'm writing my first Android app and am basically stuck. The app is a short 4 question quiz with two choices per question. This creates 16 possible results, which I've created descriptions for. I want the app to display the description corresponding to the user's responses on each of the questions after the 4th question is answered.

Right now I am storing the values in a string, which is supposed to build upon the previous string answer, after each question. So the first answer is t1, the second answer is t2, and so on. Here is the code for the second question:

public class Question2 extends Question1 implements OnClickListener
{
 private Button b1;
 private Button b2;
 public String t2;
 public TextView text2;

 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.question2);
 b1=(Button)findViewById(R.id.gamma);
 b2=(Button)findViewById(R.id.delta);
 b1.setOnClickListener(this);
 b2.setOnClickListener(this);
 text2 = (TextView)findViewById(R.id.TextView02);
 }


 public void onClick(View v) {
  Intent a = new Intent(this, Question3.class);
 if(v == b1)
 { 
  t2= t1+"gamma";
  text2.setText(t2);
 startActivity(a);
 }
  if(v == b2)
 {
 t2= t1+"delta";
 text2.setText(t2);
 startActivity(a);
 }

}
}

My problem is that the开发者_StackOverflow社区 strings won't hold the values I assign them in the previous question. I have a textview that briefly displays the values returned by the question's answer string (t2 for Question2) and it displays nullalpha or nullbeta, depending on the user's response.

How do I retain the t1's previously assigned value into Question2 so I can add to it? For instance, if t1 = "alpha" from Question1, and the user selects button b1 in Question2 how do I make t2 actually equal "alphagamma" instead of "nullgamma"? Is this even possible with strings?

Any answers and suggestions are much appreciated.


You need to rethink your whole approach to this, and get a better ground in object oriented principles. It does not make any sense for Question2 to extend Question1, they (presumably) have nothing to do with each other.

As a starting data model, it might make sense to have a single Question class, like so:

public class Question {
   private String questionText;
   private List<String> options;
   private String currentAnswer;

   public Question(String questionText, List<String> options) {
      this.questionText = questionText;
      this.options = options;
   }

   public String getQuestionText() {
      return questionText;
   }

   public String getCurrentAnswer() {
      return currentAnswer;
   }

   public void setCurrentAnswer(String answer) {
      this.currentAnswer = answer;
   }
}

You might also consider separating the answer into a different object..

Then you could create a list of questions, with one Question object for each of the 4 questions.

Rather than an activity per question, you could have a single AnswerQuestionActivity that takes in a Question (via an Intent extra), displays the question, and returns the Question object back with the answer filled in (again via the Intent extra).

When you get the result back, you can update your list of questions with the returned answer, then recalculate your display string based on the current values in the List<Question>s.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号