你的位置:首页 > 操作系统

[操作系统]解析数据用ListView来展现


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.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.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) {    // TODO Auto-generated method stub    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    getNewInfo();    lv_main_list = (ListView) findViewById(R.id.lv_main_list);    // 要保证在设置适配器时,新闻// lv_main_list.setAdapter(new MyAdapter());  }/** * 设置lv_main_list适配器 * @author Administrator * */  private class MyAdapter extends BaseAdapter {    // 得到模型曾中元素的数量,用来确定ListView需要有多少个条目    @Override    public int getCount() {      // TODO Auto-generated method stub      return newsList.size();    }    // 返回一个View对象,作为ListView的条目显示至界面    @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();        // 把布局文件中所有组件的对象封装至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);        // 把ViewHolder对象封装至View对象中        v.setTag(myHolder);      } else {        v = convertView;        myHolder = (ViewHolder) v.getTag();      }      // TODO Auto-generated method stub      // 给三个文本框设置内容      myHolder.tv_title.setText(newss.getTitle());      myHolder.tv_detail.setText(newss.getDetail());      myHolder.tv_comment.setText(newss.getComment() + "条评论");      // 给新闻图片imageview设置内容      return v;    }    class ViewHolder {      // 条目的布局文件中有什么组件,这里就定义什么组件      TextView tv_title;      TextView tv_detail;      TextView tv_comment;    }    @Override    public Object getItem(int arg0) {      // TODO Auto-generated method stub      return null;    }    @Override    public long getItemId(int arg0) {      // TODO Auto-generated method stub      return 0;    }  }  private void getNewInfo() {    // TODO Auto-generated method stub    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();            // 使用pull解析器来解析流            parseNewscatch (Exception e) {          // TODO Auto-generated catch block          e.printStackTrace();        }      }    };    t.start();  }  /** 解析*/  private void parseNews= try {      xp.setInput(is, "utf_8");      // 对节点的事件类型进行判断就可以知道当期节点是什么节点      int type = xp.getEventType();      News news = null;      while (type != switch (type) {        case 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 ("imageUrl".equals(xp.getName())) {            String imageUrl = xp.nextText();            news.setImageUrl(imageUrl);          }          break;        case if ("news".equals(xp.getName())) {            newsList.add(news);          }          break;        }        // 解析完当期的节点后,把指针移动至下个节点,并返回它的事件类型        type = xp.next();      }      for (News i : newsList) {        System.out.println(i.toString());      }      // 发消息,让主线程设置ListView的适配器,如果消息不需要携带数据的话可以发送个空消息      handler.sendEmptyMessage(1);// 数据1 表示消息发送成功的意思    } catch (Exception e) {      // TODO Auto-generated catch block      e.printStackTrace();    }  }}