MainActivity.java
public class MainActivity extends Activity {
List<News> list = null;
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == 1) {
ListView lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(new BaseAdapter() {
@Override
public int getCount() {
return list.size();
}
@Override
public View getView(int position, View convertView,
ViewGroup parent) {
View v = null;
News n = list.get(position);
ViewHolder mHolder;
if (convertView == null) {
v = View.inflate(MainActivity.this,
R.layout.item_listview, null);
mHolder = new ViewHolder();
// 把布局組件的封裝至ViewHolder對(duì)象中
mHolder.titleTv = (TextView) v
.findViewById(R.id.tv);
mHolder.detailTv = (TextView) v
.findViewById(R.id.tv_content);
mHolder.commentTv = (TextView) v
.findViewById(R.id.tv_comment);
mHolder.siv = (SmartImageView) v
.findViewById(R.id.siv);
// 把ViewHolder對(duì)象封裝至view對(duì)象中
v.setTag(mHolder);
} else {
v = convertView;
mHolder = (ViewHolder) v.getTag();
}
mHolder.titleTv.setText(n.getTitle());
mHolder.detailTv.setText(n.getDetail());
mHolder.commentTv.setText(n.getComment() + "條評(píng)論");
// 下載圖片
mHolder.siv.setImageUrl(n.getImageUrl());
return v;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public Object getItem(int position) {
return null;
}
});
}
};
};
class ViewHolder {
// 條目的布局文件中有什么組件,這里就定義什么屬性
TextView titleTv;
TextView detailTv;
TextView commentTv;
SmartImageView siv;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getNewsInfo();
}
private void getNewsInfo() {
final String path = "http://qxu1192580117.my3w.com/news.xml";
Thread thread = new Thread() {
public void run() {
URL url;
try {
url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setReadTimeout(5000);
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if (conn.getResponseCode() == 200) {
InputStream is = conn.getInputStream();
// 使用pull解析器,解析輸入流
parseNewsXml(is);
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
thread.start();
}
private void parseNewsXml(InputStream is) throws XmlPullParserException,
IOException {
News news = null;
XmlPullParser xpp = Xml.newPullParser();
xpp.setInput(is, "utf-8");
// 根據(jù)節(jié)點(diǎn)的事件類型進(jìn)行判斷,就可以知道當(dāng)前節(jié)點(diǎn)
int type = xpp.getEventType();
while (type != XmlPullParser.END_DOCUMENT) {
switch (type) {
case XmlPullParser.START_TAG:
String tagName = xpp.getName();
if ("newslist".equals(tagName)) {
list = new ArrayList<News>();
} else if ("news".equals(tagName)) {
news = new News();
} else if ("title".equals(tagName)) {
news.setTitle(xpp.nextText());
} else if ("detail".equals(tagName)) {
news.setDetail(xpp.nextText());
} else if ("comment".equals(tagName)) {
news.setComment(xpp.nextText());
} else if ("image".equals(tagName)) {
news.setImageUrl(xpp.nextText());
}
break;
case XmlPullParser.END_TAG:
String tagName2 = xpp.getName();
if ("news".equals(tagName2)) {
list.add(news);
}
break;
default:
break;
}
// 解析完當(dāng)前節(jié)點(diǎn),就把指針移動(dòng)到下一個(gè)節(jié)點(diǎn)
type = xpp.next();
}
handler.sendEmptyMessage(1);
}
}
ViewHolder性能優(yōu)化
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == 1) {
ListView lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(new BaseAdapter() {
@Override
public int getCount() {
return list.size();
}
@Override
public View getView(int position, View convertView,
ViewGroup parent) {
View v = null;
News n = list.get(position);
ViewHolder mHolder;
if (convertView == null) {
v = View.inflate(MainActivity.this,
R.layout.item_listview, null);
mHolder = new ViewHolder();
// 把布局組件的封裝至ViewHolder對(duì)象中
mHolder.titleTv = (TextView) v
.findViewById(R.id.tv);
mHolder.detailTv = (TextView) v
.findViewById(R.id.tv_content);
mHolder.commentTv = (TextView) v
.findViewById(R.id.tv_comment);
mHolder.siv = (SmartImageView) v
.findViewById(R.id.siv);
// 把ViewHolder對(duì)象封裝至view對(duì)象中
v.setTag(mHolder);
} else {
v = convertView;
mHolder = (ViewHolder) v.getTag();
}
mHolder.titleTv.setText(n.getTitle());
mHolder.detailTv.setText(n.getDetail());
mHolder.commentTv.setText(n.getComment() + "條評(píng)論");
// 下載圖片
mHolder.siv.setImageUrl(n.getImageUrl());
return v;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public Object getItem(int position) {
return null;
}
});
}
};
};
class ViewHolder {
// 條目的布局文件中有什么組件,這里就定義什么屬性
TextView titleTv;
TextView detailTv;
TextView commentTv;
SmartImageView siv;
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者