編輯:關於Android編程
Liri 30Liyue 25
package org.fun.domain; public class Person { private Integer id; private String name; private Short age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Short getAge() { return age; } public void setAge(Short age) { this.age = age; } }
package org.fun.service; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.fun.domain.Person; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class SAXPersonService { public ListgetPersons(InputStream inStream) throws Throwable { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); PersonParser personParser = new PersonParser(); parser.parse(inStream, personParser); return personParser.getPersons(); } private final class PersonParser extends DefaultHandler { private List persons = null; private String tag = null; private Person person = null; public List getPersons() { return persons; } @Override public void characters(char[] ch, int start, int length) throws SAXException { if (tag != null) { String data = new String(ch, start, length); if ("name".equals(tag)) { person.setName(data); } else if ("age".equals(tag)) { person.setAge(new Short(data)); } } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if ("person".equals(localName)) { persons.add(person); person = null; } tag = null; } @Override public void startDocument() throws SAXException { persons = new ArrayList (); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if ("person".equals(localName)) { person = new Person(); person.setId(new Integer(attributes.getValue(0))); } tag = localName; } } }
package org.fun.service; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.fun.domain.Person; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class DOMPersonService { public static ListgetPersons(InputStream inStream) throws Throwable { List persons = new ArrayList (); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(inStream); Element root = document.getDocumentElement(); NodeList personNodes = root.getElementsByTagName("person"); for (int i = 0; i < personNodes.getLength(); i++) { Person person = new Person(); Element personElement = (Element) personNodes.item(i); person.setId(new Integer(personElement.getAttribute("id"))); NodeList personChilds = personElement.getChildNodes(); for (int j = 0; j < personNodes.getLength(); j++) { if (personChilds.item(j).getNodeType() == Node.ELEMENT_NODE) { Element childElement = (Element) personChilds.item(j); if ("name".equals(childElement.getNodeName())) { person.setName(childElement.getFirstChild() .getNodeValue()); } else if ("age".equals(childElement.getNodeName())) { person.setAge(new Short(childElement.getFirstChild() .getNodeValue())); } } } persons.add(person); } return persons; } }
package org.fun.service; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import org.fun.domain.Person; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlSerializer; import android.util.Xml; public class PullPersonService { /*====================pull生成xml文件 public static void save(Listpersons, OutputStream outputStream) throws Throwable { XmlSerializer serializer = Xml.newSerializer(); serializer.setOutput(outputStream, "utf-8"); serializer.startDocument("utf-8", true); serializer.startTag(null, "persons"); for (Person person : persons) { serializer.startTag(null, "person"); serializer.attribute(null, "id", person.getId().toString()); serializer.startTag(null, "name"); serializer.text(person.getName()); serializer.endTag(null, "name"); serializer.startTag(null, "age"); serializer.text(person.getAge().toString()); serializer.endTag(null, "age"); serializer.endTag(null, "person"); } serializer.endTag(null, "persons"); serializer.endDocument(); outputStream.flush(); outputStream.close(); } =====================================*/ public static List getPersons(InputStream inStream) throws Throwable { List persons = null; Person person = null; XmlPullParser parser = Xml.newPullParser(); parser.setInput(inStream, "utf-8"); int eventType = parser.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_DOCUMENT: persons = new ArrayList (); break; case XmlPullParser.START_TAG: String name = parser.getName(); if ("person".equals(name)) { person = new Person(); person.setId(new Integer(parser.getAttributeValue(0))); } if (person != null) { if ("name".equals(name)) { person.setName(parser.nextText()); } if ("age".equals(name)) { person.setAge(new Short(parser.nextText())); } } break; case XmlPullParser.END_TAG: if ("person".equals(parser.getName())) { persons.add(person); person = null; } break; default: break; } eventType = parser.next(); } return persons; } }
在Activity,Service,Window中監聽Home鍵和返回鍵的一些思考,如何把事件傳遞出來的做法! 其實像按鍵的監聽,我相信很多人都很熟練了,我肯定也不會說這
在開發中,如果感覺系統自帶的提示框不好看,開發者可以自己定義提示框的樣式,主要是繼承Dialog 程序目錄結構 關鍵代碼 package com.dzt.custo
前言由於大部分android初級教程都沒教怎麼選擇時間,初學者碰到這種難免會有些不知所措,難道要讓用戶自己輸入日期時間?先不說用戶體驗不好,處理用戶輸入各式各樣的日期格式
Tab與TabHost:這就是Tab,而盛放Tab的容器就是TabHost 。如何實現?? 每一個Tab還對應了一個布局,這個就有點好玩了。一個Activity,對應了多