編輯:關於Android編程
android中已經集成了pull解釋器,所以不用添加jar文件,如果在javaee中使用pull需要手動添加pull的jar文件。android系統本身就是通過pull解釋器來處理xml文件。所以說明pull還是有一定的優越性。
1、main.xml
[html]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
2、item.xml
[html]
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent" www.2cto.com
android:layout_height="wrap_content" />
3、我將person.xml文件放到了android的assets文件夾下
[html]
<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person id="23">
<name>jack</name>
<age>16</age>
</person>
<person id="24">
<name>tom</name>
<age>15</age>
</person>
</persons>
4、MainActivity.java
[java]
public class MainActivity extends Activity {
private PersonService ps = null;
private ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ps = new PersonService();
lv = (ListView) this.findViewById(R.id.lv);
try {
AssetManager am = getAssets();
InputStream is = am.open("person.xml");
List<Person> list = ps.getPersons(is);
List<String> names = new ArrayList<String>();
for(Person p:list){
String name = p.getName();
names.add(name);
System.out.println(name);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.item, 0,names);
lv.setAdapter(adapter);
} catch (Exception e) {
e.printStackTrace();
}
}
}
5、PersonService.java
[java]
public class PersonService {
public List<Person> getPersons(InputStream xml) throws Exception{
XmlPullParser pullParser = Xml.newPullParser();
List<Person> persons = null;
Person person = null;
pullParser.setInput(xml, "utf-8");
int event = pullParser.getEventType();
//判斷是否解釋到文檔結束部分
while (event != XmlPullParser.END_DOCUMENT) {
switch (event) {
case XmlPullParser.START_DOCUMENT:
persons = new ArrayList<Person>();
break;
case XmlPullParser.START_TAG:
if("person".equals(pullParser.getName())){
//得到標簽中的第一個參數的值
int id = Integer.parseInt(pullParser.getAttributeValue(0));
person = new Person();
person.setId(id);
}
if("name".equals(pullParser.getName())){
//得到標簽中間的文本內容
String name = pullParser.nextText();
person.setName(name);
}
if("age".equals(pullParser.getName())){
int age = Integer.parseInt(pullParser.nextText());
person.setAge(age);
}
break;
case XmlPullParser.END_TAG://如果解釋標簽是perosn就將person對象存放到list中
if("person".equals(pullParser.getName())){
persons.add(person);
}
break;
case XmlPullParser.END_DOCUMENT:
break;
default:
break;
}
//pull不會主動向後執行,所以自己調用next方法並把值付給event
event = pullParser.next();
}
System.out.println("persons.size()-->"+persons.size());
return persons;
}
}
6、Person.java
[java]
public class Person {
private Integer id;
private String name;
private Integer 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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
從今天起,我要分析React Native for Android的源碼。本系列主要是從使用和源碼分析的角度來一步步的剖析React Native Android。當然,
先看效果圖主要處理的地方:1、RecyclerView中Adapter的item個人可以無限輪詢.2、RecyclerView自動滑動3、手指按下時滑動停止,手指抬起後繼
本文實例為大家分享了Android仿新浪微博分頁管理界面的具體代碼,供大家參考,具體內容如下多個activity分頁管理,為了方便獲取上下文,采用繼承TabActivit
本文實例講述了Android TextView中文字通過SpannableString設置屬性的方法。分享給大家供大家參考,具體如下:在Android中,TextView