CS 175/286 - Lecture 5

Cover page image

Cay S. Horstmann

Lecture 5 Quiz 1

Consider an application that gives a quiz to the user. Are the quiz questions a part of

Lecture 5 Quiz 2

  • In Example 6.2, how does one get a reference to the red button?
  • Today's Lecture

    Transferring Data to the Target Activity


    List Adapters

    Reading Before Next Class


    Transferring Data to the Target Activity

    1. Continue with the code from the lab of lecture 4.
    2. Right now, all the buttons do the same thing. We want each button to bring relevant information to the target activity so that it can do it's job. We'll overdesign the application a bit so that you can use the design in the future.
    3. We want to transfer domain objects. Here is a Question class. Add it to your project. Turn off the warning in Window → Preferences → Java → Compiler → Errors/Warnings → Potential programming problems → Serializable class without serialVersionUID.
    4. Add an instance variable private Question question to MainActivity. Change the code to construct a new Question(title, choice1, choice2, choice3, choice4), and set the question instance variable. (The correct choice should start with a *.) Read the text and button labels from the Question object.
    5. Try your program. It should still work.
    6. Now use putExtra to add the question and the index (0, 1, 2, 3) of the current button to the intent. The latter is a bit tricky. Use this outline:
      for (int i = 0; i < buttonIds.length; i++) {
          final int choiceIndex = i;
          ((Button) findViewById(buttonIds[i])).setOnClickListener(
              new View.OnClickListener() {
          	    @Override public void onClick(View arg0) {
                  Intent intent = new Intent(MainActivity.this, ResponseActivity.class);
                  intent.putExtra("choiceIndex", choiceIndex);
    7. To see why you need the final, try
      intent.putExtra("choiceIndex", choiceIndex)
      instead. What happens?
    8. Now capture the information on the target activity. Call getIntExtra and getSerializableExtra. Then call
      ((TextView) findViewById(R.id.responseText)).setText(
          question.isCorrect(choiceIndex) ? "Good job!" : "Try again!");
    9. Try it out. What happens?

    Displaying the Choices in a List

    1. Make a layout containing just a text view. (File → New → Other → Android → Android XML Layout file)
    2. Remove the buttons and all but the top text view from the main layout, and instead put in a list view below the question text.
    3. Comment out the button handler for now and replace the text field initialization with the initialization of the list view. Make an ArrayAdapter out of the question choices. What is your code?
    4. What happens when your application runs?
    5. What happens when you select one of the questions?
    6. Now add an OnItemClickListener. Move the appropriate code from your button listener. What is your code?
    7. Now what happens when you run and select one of the questions?