Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

以 ListView 展示解析後的網路數據

編輯:關於 Android 開發

以 ListView 展示解析後的網路數據

本文示範一個完整的流程:從網路下載 XML 數據,使用 XmlPullParser 解析後,透過 ListView 搭配自訂 Adapter 呈現新聞列表。由於網路請求與解析較耗時,需在子執行緒中進行,完成後再透過 Handler 通知主執行緒更新介面。

完整程式碼如下:

package com.org.demo.wangfeng; 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 com.org.demo.wangfeng.demo.News; import com.org.wangfeng.R; import android.annotation.SuppressLint; 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; public class MainActivity extends Activity { private String path = ""; List<News> newsList; private ListView lv_main_list; @SuppressLint("HandlerLeak") Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { lv_main_list.setAdapter(new MyAdapter()); }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); getNewInfo(); lv_main_list = (ListView) findViewById(R.id.lv_main_list); } private class MyAdapter extends BaseAdapter { @Override public int getCount() { return newsList.size(); } @Override public View getView(int positon, View convertView, ViewGroup parent) { News newss = newsList.get(positon); View v; ViewHolder myHolder; if (convertView == null) { v = View.inflate(MainActivity.this, R.layout.mainlist_item, null); myHolder = new ViewHolder(); myHolder.tv_title = (TextView) v.findViewById(R.id.tv_main_title); myHolder.tv_detail = (TextView) v.findViewById(R.id.tv_main_detail); myHolder.tv_comment = (TextView) v.findViewById(R.id.tv_main_comment); v.setTag(myHolder); } else { v = convertView; myHolder = (ViewHolder) v.getTag(); } myHolder.tv_title.setText(newss.getTitle()); myHolder.tv_detail.setText(newss.getDetail()); myHolder.tv_comment.setText(newss.getComment() + "條評論"); return v; } class ViewHolder { TextView tv_title; TextView tv_detail; TextView tv_comment; } @Override public Object getItem(int arg0) { return null; } @Override public long getItemId(int arg0) { return 0; } } private void getNewInfo() { Thread t = new Thread() { @Override public void run() { try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); conn.connect(); if (conn.getResponseCode() == 200) { InputStream is = conn.getInputStream(); parseNewsXml(is); } } catch (Exception e) { 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())) { news.setTitle(xp.nextText()); } else if ("detail".equals(xp.getName())) { news.setDetail(xp.nextText()); } else if ("comment".equals(xp.getName())) { news.setComment(xp.nextText()); } else if ("imageUrl".equals(xp.getName())) { news.setImageUrl(xp.nextText()); } break; case XmlPullParser.END_TAG: if ("news".equals(xp.getName())) { newsList.add(news); } break; } type = xp.next(); } handler.sendEmptyMessage(1); } catch (Exception e) { e.printStackTrace(); } } }

在 Adapter 中,透過 ViewHolder 模式快取列表項中的視圖元件,可有效減少 findViewById 的呼叫次數、提升滾動流暢度。

列表項的版面配置如下:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/iv_main_image" android:layout_width="70dp" android:layout_height="85dp" android:contentDescription="@null" android:paddingBottom="2dp" android:paddingTop="3dp" android:scaleType="fitXY" android:src="@drawable/d" /> <TextView android:id="@+id/tv_main_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/iv_main_image" android:singleLine="true" android:text="這是大標題" android:textSize="23sp" /> <TextView android:id="@+id/tv_main_detail" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/iv_main_image" android:layout_toRightOf="@id/iv_main_image" android:lines="2" android:text="這是正文" android:textColor="@android:color/darker_gray" android:textSize="14sp" /> <TextView android:id="@+id/tv_main_comment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@id/tv_main_detail" android:text="評論書" android:textColor="#ff0000" /> </RelativeLayout>
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved