CS 185C/286 - Lecture 4

Cover page image

Cay S. Horstmann

Lecture 4 Quiz 1

Put your answer into Piazza.

In which Activity method should you save user edits?

  1. onPause
  2. onStop
  3. onDestroy
  4. all of them

Lecture 4 Quiz 2

Suppose you write an e-book reader. What happens when the user hits the back button, assuming that you didn't override the default behavior?

  1. Nothing.
  2. The previous book page is displayed.
  3. The previous activity in your application is displayed
  4. The previous user activity is displayed (which may be in a different application)

Today's Lecture/Lab

Lists

List Adapters

XML Parsing

SAX Parsing

SAX Parsing Example

public class Question implements Serializable {
    private String text;
    private List<String> choices = new ArrayList<String>();
    private int correctChoice;

    public void readFromXml(InputStream in) throws IOException {
        class QuestionHandler extends BasicHandler {
            @Override
            public void startElement(String uri, String localName,
                    String qName, Attributes attributes) throws SAXException {
                super.startElement(uri, localName, qName, attributes);
                if ("true".equals(attributes.getValue("value")))
                    correctChoice = choices.size(); 
            }
            @Override
            public void endElement(String uri, String localName, String qName)
                    throws SAXException {
                if (qName.equals("text")) text = lastString();
                else if (qName.equals("choice")) choices.add(lastString());
            }
        }
        
        try {       
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser(); 
            parser.parse(in, new QuestionHandler());
        } catch (Exception ex) {
            IOException ioEx = new IOException(ex.getMessage());
            ioEx.initCause(ex);
            throw ioEx;
        }       
    }
	  ...
}

Reading Before Next Class

Lab

Displaying the Choices in a List

  1. Continue with the code from lab 3.
  2. Tip: Have a look in AndroidManifest.xml. You can use android:name=".MainActivity" instead of android:name="packageName.MainActivity". A name starting with a period is relative to the package attribute of the manifest element. That way, you don't have to change so many things when you rename your package.
  3. Make a layout containing just a text view. (File → New → Other → Android → Android XML Layout file)
  4. 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.
  5. Comment out the button handler for now and replace the text field initialization in onPostExecute with the initialization of the list view. Make an ArrayAdapter out of the question choices. What is your code?
  6. What happens when your application runs?
  7. What happens when you select one of the questions?
  8. Now add an OnItemClickListener. Move the appropriate code from your button listener. What is your code?
  9. Now what happens when you run and select one of the questions?

Reading XML