Put your answer into Piazza.
In which Activity
method should you save user edits?
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?
ListView
into the activity XMLAdapter
to populate listOnItemClickListener
to get notified of item selectionvoid onItemClick(AdapterView<?> parent, View view, int position, long id)
new ArrayAdapter<String>(activity, itemLayoutId, strings)
TextView
getView(int, View, ViewGroup)
in subclasspublic View getView(int position, View v, ViewGroup parent) { if (v == null) { LayoutInflater vi = LayoutInflater.from(getContext()); v = vi.inflate(R.layout.itemLayoutId, parent); } Item item = items.get(position); ((TextView) v.findViewById(R.id.description)).setText(item.getDescription()); ((TextView) v.findViewById(R.id.price)).setText(item.getPrice()); return v; }
<img src='hamster.jpg'/>
<p>Hello</p>
<ul><li>...</li><li>...</li></ul>
void startElement(String uri, String localName, String qName, Attributes attributes)
void endElement(String uri, String localName, String qName)
void characters(char[] ch, int start, int length)
characters
can be called multiple times. Use my BasicHandler
convenience class.BasicHandler
. Add fields for whatever you want to collect, or make inner class in the object that you are building.startElement
endElement
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; } } ... }
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.onPostExecute
with the initialization of the list view. Make an ArrayAdapter
out of the question choices. What is your code?OnItemClickListener
. Move the appropriate code from your button listener. What is your code?BasicHandler
to your project.readFromXml
method from the slide to the Question
class.doInBackground
. What is your code?readFromXml
more closely. Why does the startElement
method call super.startElement
but the endElement
doesn't?startElement
method look at qName
?if
condition in startElement
attributes.getValue("value").equals("true")Try it out if you don't know.