CS 185C/286 - Lecture 3

Cover page image

Cay S. Horstmann

Lecture 3 Quiz 1

Put your answer into Piazza.

In the DO NOT USE!!! version of the initGame example, which call(s) take so long that they should be called outside the UI thread?

  1. start
  2. initialize
  3. stop
  4. All of them

Lecture 3 Quiz 2

The author makes us very nervous about the mutable Map<String, String> vals, without telling us how to fix the problem. What fix would work?

  1. Calling clear in onPostUpdate
  2. Calling clear at the end of doInBackground
  3. Passing vals.clone() to execute
  4. All of them

Lecture 3 Quiz 3

What is your opinion about homework 1?

  1. Kind of easy
  2. Just right
  3. Too hard
  4. What homework?

Today's Lecture/Lab

Making buttons work

Changing Activities

Transferring Data to the Target Activity

Command-line Builds

Reading Before Next Class


Making the Buttons Active

  1. Start with the solution to the first homework. Solutions can be found in the solutions repo on zoidberg, and all of you have read permissions. Clone that repo somewhere outside your cs185c repo. How do you do that?
  2. How did you know how to do that? (Pat yourself in the back if your response was: “Because I looked it up in my git notes file”.)
  3. Now copy the code into the cs185c directory:
    cp -R solutions/workspace/hw01 cs185c/workspace/lab03
  4. In Eclipse, select File → Import → General → Existing Projects into Workspace, and select lab03. Then rename the package to edu.sjsu.cs185c.lab03 in the Package Explorer. Also rename the package in AndroidManifest.xml and change the app name to Lab 3 in strings.xml. (You should make it a habit to do this whenever you make a new lab or homework assignment.)
  5. While we are at it, let's banish tabs from our source files. (Don't know why? Read this.) Go to Window → Preferences and type tabs into the search box in the top left corner. Click on Text Editors and click on Insert spaces for tabs. But wait, that's not all. Now click on Java/Editor/Typing, then on the Formatter link, and on Edit. Change the name from Eclipse (default) to CS185C, and change the Tabs poliy to Spaces only. Click on Apply and Ok. Go to MainActivity.java and select Source → Correct Indentation from the menu. Verify that there are now spaces in the file (by deleting a character in an indented line).
  6. Select File → New → Other → Android → Android Activity and make a ResponseActivity with all the default options.
  7. In the MainActivity, make the first button go to the ResponseActivity when clicked. How did you do that?
  8. What happens when you run the program and click the button?
  9. What happens when you click the Back button in the emulator?
  10. Now make all buttons go to the ResponseActivity. Use a for loop like in the onPostExecute method.

Transferring Data to the Target Activity

  1. 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.
  2. 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.
  3. Add an instance variable private Question question to MainActivity. Change the code in doInBackground to construct a new Question(), call read(in), and set the question instance variable. Remove the ArrayList<String>. Change the return type to Void and return null.
  4. Change onPostExecute so that it gets the question text and choices from the question object.
  5. Change the URL to point to this file. The * denotes the correct answer.
  6. Try your program. It should still work.
  7. 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);
  8. To see why you need the final, try
    intent.putExtra("choiceIndex", choiceIndex)
    instead. What happens?
  9. 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!");
  10. Try it out. What happens?

Building a Project with Ant

  1. Download Ant from here and uncompress into your home directory.
  2. Edit ~/.bashrc and add the lines
    export ANT_HOME=/home/user/apache-ant-1.8.4
    export PATH=$PATH:$ANT_HOME/bin
    export ANDROID_HOME=/home/user/adt-bundle-linux-x86/sdk
    export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools
  3. Close Eclipse. Open a new command shell and run
    cd cs185c/workspace/lab03
    android update project --target 1 --path .
    android avd &
    Wait until the emulator as loaded and run
    ant debug install
  4. Where is your app? Hint: Click on the app list button (in the middle of the bottom of the home screen).