您的位置:首页 > 移动开发 > Android开发

Android学习第十二天---DOM+Handler

2013-03-20 01:00 204 查看
<RelativeLayout 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"
tools:context=".MainActivity" >

<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content">

</ListView>

</RelativeLayout>


java

package cn.will.test;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import cn.core.entity.Books;
import cn.core.service.BookPrase;

public class MainActivity extends Activity {

private List<String> list= new ArrayList<String>();
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.what==0)
{
List<Books> data=(List) msg.obj;
for (Books books : data) {
list.add(books.getBookId()+"--"+books.getBookName());

}
((ListView)findViewById(R.id.listView1)).setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,list));
}

}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

new Thread(new Runnable() {

@Override
public void run() {

InputStream is=getResources().openRawResource(R.raw.books);
Message message = new Message();
message.what=0;
List<Books> data=BookPrase.prase(is);
message.obj=data;
handler.sendMessage(message);
}
}).start();

}

}


实体类

package cn.core.entity;

public class Books {
private Integer bookId;
private String bookName;
public Books() {
super();
}
public Books(Integer bookId, String bookName) {
super();
this.bookId = bookId;
this.bookName = bookName;
}
public Integer getBookId() {
return bookId;
}
public void setBookId(Integer bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}

}


控制层

package cn.core.entity;

public class Books {
private Integer bookId;
private String bookName;
public Books() {
super();
}
public Books(Integer bookId, String bookName) {
super();
this.bookId = bookId;
this.bookName = bookName;
}
public Integer getBookId() {
return bookId;
}
public void setBookId(Integer bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: