使用listView的时候,通过ViewHolder进行缓存可以提升性能
JavaBean,创建了News对象的几个参数title,detail,comment,imageUrl
package com.ldw.newsView.domain;
public class News {
private String title;
private String detail;
private String comment;
private String iamgeUrl;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public String getIamgeUrl() {
return iamgeUrl;
}
public void setIamgeUrl(String iamgeUrl) {
this.iamgeUrl = iamgeUrl;
}
@Override
public String toString() {
return "News [title=" + title + ", detail=" + detail + ", comment="
+ comment + ", iamgeUrl=" + iamgeUrl + "]";
}
}
布局文件
activity_main.xml主布局,用ListView进行填充
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
ListView的布局文件,每一个的布局
item_listview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.loopj.android.image.SmartImageView
android:id="@+id/siv"
android:layout_height="90sp"
android:layout_width="70sp"
android:src="@drawable/ic_launcher"
android:layout_centerVertical="true"
/>
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="XX"
android:layout_toRightOf="@id/siv"
android:textSize="22sp"
android:singleLine="true"
/>
<TextView
android:id="@+id/tv_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Xomatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'ldd'omatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'ldd'X"
android:layout_toRightOf="@id/siv"
android:textSize="14sp"
android:textColor="@android:color/darker_gray"
android:layout_below="@id/tv_title"
android:lines="2"
/>
<TextView
android:id="@+id/tv_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="213评论"
android:textColor="#ff0000"
android:layout_alignParentRight="true"
android:layout_below="@id/tv_detail"
/>
</RelativeLayout>
主布局文件,首先调用getNewsInfo利用子线程来请求xml并解析xml文件中的元素,同时把元素放在List<News>对象中,然后在setAdapter中来通过ViewHolder来获取到List中的News对象来解析每一条的元素,并进行显示
package com.ldw.newsView;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Xml;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.ldw.newsView.domain.News;
import com.loopj.android.image.SmartImageView;
public class MainActivity extends Activity {
List<News> newsList;
Handler handler = new Handler(){
public void handleMessage(android.os.Message msg){
ListView lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(new MyAdapter());
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getNewsInfo();
//ListView lv = (ListView) findViewById(R.id.lv);
//需保证解析结束以后,再设置适配器
//lv.setAdapter(new MyAdapter());
}
class MyAdapter extends BaseAdapter{
//得到元素的数量,确定listView条目的数量
@Override
public int getCount() {
// TODO Auto-generated method stub
return newsList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
News news = newsList.get(position);
View v = null;
ViewHolder mHolder;
if(convertView == null){
v = View.inflate(MainActivity.this, R.layout.item_listview, null);
//布局文件中所有组件的对象封装到ViewHolder对象中
mHolder = new ViewHolder();
mHolder.tv_title = (TextView) v.findViewById(R.id.tv_title);
mHolder.tv_detail = (TextView) v.findViewById(R.id.tv_detail);
mHolder.tv_comment = (TextView) v.findViewById(R.id.tv_comment);
mHolder.siv = (SmartImageView) v.findViewById(R.id.siv);
//把ViewHolder对象封装到View对象中
v.setTag(mHolder);
}
else{
v = convertView;
mHolder = (ViewHolder) v.getTag();
}
mHolder.tv_title.setText(news.getTitle());
mHolder.tv_detail.setText(news.getDetail());
mHolder.tv_comment.setText(news.getComment() + "条评论");
mHolder.siv.setImageUrl(news.getIamgeUrl());
/*
TextView tv_title = (TextView) v.findViewById(R.id.tv_title);
tv_title.setText(news.getTitle());
TextView tv_detail = (TextView) v.findViewById(R.id.tv_detail);
tv_detail.setText(news.getDetail());
TextView tv_comment = (TextView) v.findViewById(R.id.tv_comment);
tv_comment.setText(news.getComment() + "条评论");
SmartImageView siv = (SmartImageView) v.findViewById(R.id.siv);
siv.setImageUrl(news.getIamgeUrl());
*/
return v;
}
class ViewHolder{
//条目的布局文件中有什么组件,这里就定义有什么属性
TextView tv_title;
TextView tv_comment;
TextView tv_detail;
SmartImageView siv;
}
}
private void getNewsInfo(){
Thread t = new Thread(){
@Override
public void run(){
String path="http://192.168.0.102:8080/news.txt";
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
//发送http请求,获取相应的状态吗
if(conn.getResponseCode() == 200){
InputStream is = conn.getInputStream();
//使用pull解析器解析is流
parseNewsXml(is);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
t.start();
}
private void parseNewsXml(InputStream is){
XmlPullParser xp = Xml.newPullParser();
try {
xp.setInput(is, "utf-8");
//对节点的事件类型进行判断,可以知道当前的节点类型
int type = xp.getEventType();
News news = null;
while(type != XmlPullParser.END_DOCUMENT){
switch(type){
case XmlPullParser.START_TAG:
if("newsList".equals(xp.getName())){
newsList = new ArrayList<News>();
}
else if("news".equals(xp.getName())){
news = new News();
}
else if("title".equals(xp.getName())){
String title = xp.nextText();
news.setTitle(title);
}
else if("detail".equals(xp.getName())){
String detail = xp.nextText();
news.setDetail(detail);
}
else if("comment".equals(xp.getName())){
String comment = xp.nextText();
news.setComment(comment);
}
else if("image".equals(xp.getName())){
String image = xp.nextText();
news.setIamgeUrl(image);
}
break;
case XmlPullParser.END_TAG:
if("news".equals(xp.getName())){
newsList.add(news);
}
break;
}
//解析玩借点以后,把指针移动到下一个节点,返回他的时间类型
type = xp.next();
}
//发消息让主线程设置listView适配器,因为消息不需要携带数据,发送空消息
handler.sendEmptyMessage(1);
for(News n : newsList){
System.out.println(n.toString());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}