Android——activity生命周期Demo
http://www.eoeandroid.com/thread-207556-1-1.html
Android——service生命周期Demo
http://www.eoeandroid.com/thread-207558-1-1.html
android spinner 基础小实例
http://www.eoeandroid.com/thread-207188-1-1.html
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width@H_502_12@="fill_parent"
android:layout_height@H_502_12@="fill_parent"
android:orientation@H_502_12@="vertical" >
<TextView
android:layout_width@H_502_12@="fill_parent"
android:layout_height@H_502_12@="wrap_content"
android:text@H_502_12@="@string/hello" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World,MainActivity!</string> <string name="app_name">XML解析</string> </resources>
在src下新建的Person.xml
<?xml version="1.0" encoding="UTF-8"?> <persons> <person id="23"> <name>LiMing</name> <age>30</age> </person> <person id="20"> <name>ZhangSan</name> <age>25</age> </person> </persons>
package@H_502_12@ cn.itcast.domain;
@H_502_12@public@H_502_12@ class@H_502_12@ Person {
@H_502_12@private@H_502_12@ Integer id;
@H_502_12@private@H_502_12@ String name;
@H_502_12@private@H_502_12@ Integer age;
@H_502_12@public@H_502_12@ Integer getId() {
@H_502_12@return@H_502_12@ id;
}
@H_502_12@public@H_502_12@ void@H_502_12@ setId(Integer id) {
@H_502_12@this@H_502_12@.id = id;
}
@H_502_12@public@H_502_12@ String getName() {
@H_502_12@return@H_502_12@ name;
}
@H_502_12@public@H_502_12@ void@H_502_12@ setName(String name) {
@H_502_12@this@H_502_12@.name = name;
}
@H_502_12@public@H_502_12@ Integer getAge() {
@H_502_12@return@H_502_12@ age;
}
@H_502_12@public@H_502_12@ void@H_502_12@ setAge(Integer age) {
@H_502_12@this@H_502_12@.age = age;
}
@Override
@H_502_12@public@H_502_12@ String toString() {
@H_502_12@return@H_502_12@ "Person [id=" + id + ",name=" + name + ",age=" + age + "]";
}
@H_502_12@public@H_502_12@ Person(){
}
@H_502_12@public@H_502_12@ Person(Integer id,String name,Integer age) {
@H_502_12@super@H_502_12@();
@H_502_12@this@H_502_12@.id = id;
@H_502_12@this@H_502_12@.name = name;
@H_502_12@this@H_502_12@.age = age;
}
}
@H_502_12@
package@H_502_12@ cn.itcast.xml;
@H_502_12@import@H_502_12@ android.app.Activity;
@H_502_12@import@H_502_12@ android.os.Bundle;
@H_502_12@public@H_502_12@ class@H_502_12@ MainActivity extends@H_502_12@ Activity {
@H_502_12@/**@H_502_12@ Called when the activity is first created. @H_502_12@*/@H_502_12@
@Override
@H_502_12@public@H_502_12@ void@H_502_12@ onCreate(Bundle savedInstanceState) {
@H_502_12@super@H_502_12@.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}@H_502_12@
package@H_502_12@ cn.itcast.service;
@H_502_12@import@H_502_12@ java.io.InputStream;
@H_502_12@import@H_502_12@ java.io.OutputStream;
@H_502_12@import@H_502_12@ java.util.ArrayList;
@H_502_12@import@H_502_12@ java.util.List;
@H_502_12@import@H_502_12@ org.xmlpull.v1.XmlPullParser;
@H_502_12@import@H_502_12@ org.xmlpull.v1.XmlPullParserFactory;
@H_502_12@import@H_502_12@ org.xmlpull.v1.XmlSerializer;
@H_502_12@import@H_502_12@ android.util.Xml;
@H_502_12@import@H_502_12@ cn.itcast.domain.Person;
@H_502_12@public@H_502_12@ class@H_502_12@ PersonService {
@H_502_12@/**@H_502_12@
* 获取数据
* @H_502_12@@param@H_502_12@ xml
* @H_502_12@@return@H_502_12@
* @H_502_12@@throws@H_502_12@ Exception
@H_502_12@*/@H_502_12@
public@H_502_12@ static@H_502_12@ List<Person> getPerson(InputStream xml) throws@H_502_12@ Exception{
List@H_502_12@<Person> persons = null@H_502_12@;
Person person @H_502_12@= null@H_502_12@;
@H_502_12@//@H_502_12@第一种写法
@H_502_12@//@H_502_12@ XmlPullParser pullParser = XmlPullParserFactory.newInstance().newPullParser();
@H_502_12@//@H_502_12@第二种写法@H_502_12@
XmlPullParser pullParser @H_502_12@= Xml.newPullParser();
pullParser.setInput(xml,@H_502_12@"UTF-8"); //@H_502_12@为Pull解析器设置要解析的XML数据@H_502_12@
int@H_502_12@ event = pullParser.getEventType();
@H_502_12@while@H_502_12@(event != XmlPullParser.END_DOCUMENT){
@H_502_12@switch@H_502_12@(event){
@H_502_12@case@H_502_12@ XmlPullParser.START_DOCUMENT:
persons @H_502_12@= new@H_502_12@ ArrayList<Person>();
@H_502_12@break@H_502_12@;
@H_502_12@case@H_502_12@ XmlPullParser.START_TAG:
@H_502_12@if@H_502_12@("person".equals(pullParser.getName())){
@H_502_12@int@H_502_12@ id = new@H_502_12@ Integer(pullParser.getAttributeValue(0));
person @H_502_12@= new@H_502_12@ Person();
person.setId(id);
}
@H_502_12@if@H_502_12@("name".equals(pullParser.getName())){
String name @H_502_12@= pullParser.nextText();
person.setName(name);
}
@H_502_12@if@H_502_12@("age".equals(pullParser.getName())){
@H_502_12@int@H_502_12@ age = new@H_502_12@ Integer(pullParser.nextText());
person.setAge(age);
}
@H_502_12@break@H_502_12@;
@H_502_12@case@H_502_12@ XmlPullParser.END_TAG:
@H_502_12@if@H_502_12@("person".equals(pullParser.getName())){
persons.add(person);
}
@H_502_12@break@H_502_12@;
}
event @H_502_12@= pullParser.next();
}
@H_502_12@return@H_502_12@ persons;
}
@H_502_12@/**@H_502_12@
* 保存数据到XML文件中
* @H_502_12@@param@H_502_12@ persons 数据
* @H_502_12@@param@H_502_12@ out 输出方向
* @H_502_12@@throws@H_502_12@ Exception
@H_502_12@*/@H_502_12@
public@H_502_12@ static@H_502_12@ void@H_502_12@ save(List<Person> persons,OutputStream out) throws@H_502_12@ Exception{
XmlSerializer serializer @H_502_12@= Xml.newSerializer(); //@H_502_12@得到序列化器@H_502_12@
serializer.setOutput(out,@H_502_12@"UTF-8");
serializer.startDocument(@H_502_12@"UTF-8",true@H_502_12@); //@H_502_12@true--是否该文件单独存在 @H_502_12@
serializer.startTag(null@H_502_12@,"persons");
@H_502_12@for@H_502_12@(Person person : persons){
serializer.startTag(@H_502_12@null@H_502_12@,"person");
serializer.attribute(@H_502_12@null@H_502_12@,"id",person.getId().toString());
serializer.startTag(@H_502_12@null@H_502_12@,"name");
serializer.text(person.getName());
serializer.endTag(@H_502_12@null@H_502_12@,"name");
serializer.startTag(@H_502_12@null@H_502_12@,"age");
serializer.text(person.getAge().toString());
serializer.endTag(@H_502_12@null@H_502_12@,"age");
serializer.endTag(@H_502_12@null@H_502_12@,"person");
}
serializer.endTag(@H_502_12@null@H_502_12@,"persons");
serializer.endDocument();
out.flush();
out.close();
}
}
@H_502_12@
单元测试:
package@H_502_12@ cn.itcast.test;
@H_502_12@import@H_502_12@ java.io.File;
@H_502_12@import@H_502_12@ java.io.FileOutputStream;
@H_502_12@import@H_502_12@ java.io.InputStream;
@H_502_12@import@H_502_12@ java.util.ArrayList;
@H_502_12@import@H_502_12@ java.util.Iterator;
@H_502_12@import@H_502_12@ java.util.List;
@H_502_12@import@H_502_12@ cn.itcast.domain.Person;
@H_502_12@import@H_502_12@ cn.itcast.service.PersonService;
@H_502_12@import@H_502_12@ android.test.AndroidTestCase;
@H_502_12@import@H_502_12@ android.util.Log;
@H_502_12@public@H_502_12@ class@H_502_12@ PersonServiceTest extends@H_502_12@ AndroidTestCase{
@H_502_12@private@H_502_12@ static@H_502_12@ final@H_502_12@ String TAG = "PersonServiceTest";
@H_502_12@public@H_502_12@ void@H_502_12@ testPersons() throws@H_502_12@ Exception{
InputStream xml @H_502_12@= this@H_502_12@.getClass().getClassLoader().getResourceAsStream("person.xml");
List@H_502_12@<Person> persons = PersonService.getPerson(xml);
@H_502_12@for@H_502_12@(Person person : persons){
Log.i(TAG,person.toString());
}
}
@H_502_12@public@H_502_12@ void@H_502_12@ testSave() throws@H_502_12@ Exception{
List@H_502_12@<Person> persons = new@H_502_12@ ArrayList<Person>();
persons.add(@H_502_12@new@H_502_12@ Person(1,"zhangsan",80));
persons.add(@H_502_12@new@H_502_12@ Person(2,"lisi",43));
persons.add(@H_502_12@new@H_502_12@ Person(3,"wangwu",12));
@H_502_12@//@H_502_12@<包> @H_502_12@//@H_502_12@/file@H_502_12@
File xmlFile @H_502_12@= new@H_502_12@ File(getContext().getFilesDir(),"itcast.xml");
FileOutputStream outStream @H_502_12@= new@H_502_12@ FileOutputStream(xmlFile);
PersonService.save(persons,outStream);
}
}
@H_502_12@