android项目之RRS阅读器

来源:互联网 发布:cyberlink是什么软件 编辑:程序博客网 时间:2024/06/05 09:50

ps:本程序是android网络技术开发详解书中的代码

项目截图:

代码片段: 

适配器布局的优化

public class MyAdapter extends BaseAdapter{  private LayoutInflater mInflater;  private List<News> items;  public MyAdapter(Context context, List<News> it)  {    mInflater = LayoutInflater.from(context);    items = it;  }  @Override  public int getCount()  {    return items.size();  }  @Override  public Object getItem(int position)  {    return items.get(position);  }  @Override  public long getItemId(int position)  {    return position;  }  @Override  public View getView(int position, View convertView, ViewGroup par)  {    ViewHolder holder;    if (convertView == null)    {      /* 使用自定义的news_row作为Layout */      convertView = mInflater.inflate(R.layout.news_row, null);      /* 初始化holder的text与icon */      holder = new ViewHolder();      holder.text = (TextView) convertView.findViewById(R.id.text);      convertView.setTag(holder);    } else    {      holder = (ViewHolder) convertView.getTag();    }    News tmpN = (News) items.get(position);    holder.text.setText(tmpN.getTitle());    return convertView;  }  /* class ViewHolder */  private class ViewHolder  {    TextView text;  }}


XML的解析

package dfzy.RSSCH;import java.util.ArrayList;import java.util.List;import org.xml.sax.Attributes;import org.xml.sax.SAXException;import org.xml.sax.helpers.DefaultHandler;public class MyHandler extends DefaultHandler{  private boolean in_item = false;  private boolean in_title = false;  private boolean in_link = false;  private boolean in_desc = false;  private boolean in_date = false;  private boolean in_mainTitle = false;  private List<News> li;  private News news;  private String title = "";  private StringBuffer buf = new StringBuffer();  public List<News> getParsedData()  {    return li;  }  /* 将解析出的RSS title返回 */  public String getRssTitle()  {    return title;  }  /* XML文件开始解析时调用此方法 */  @Override  public void startDocument() throws SAXException  {    li = new ArrayList<News>();  }  /* XML文件结束解析时调用此方法 */  @Override  public void endDocument() throws SAXException  {  }  /* 解析到Element的开头时调用此方法 */  @Override  public void startElement(String namespaceURI, String localName,      String qName, Attributes atts) throws SAXException  {    if (localName.equals("item"))    {      this.in_item = true;      /* 解析到item的开头时new一个News对象 */      news = new News();    } else if (localName.equals("title"))    {      if (this.in_item)      {        this.in_title = true;      } else      {        this.in_mainTitle = true;      }    } else if (localName.equals("link"))    {      if (this.in_item)      {        this.in_link = true;      }    } else if (localName.equals("description"))    {      if (this.in_item)      {        this.in_desc = true;      }    } else if (localName.equals("pubDate"))    {      if (this.in_item)      {        this.in_date = true;      }    }  }  /* 解析到Element的结尾时调用此方法 */  @Override  public void endElement(String namespaceURI, String localName,      String qName) throws SAXException  {    if (localName.equals("item"))    {      this.in_item = false;      /* 解析到item的结尾时将News对象写入List中 */      li.add(news);    } else if (localName.equals("title"))    {      if (this.in_item)      {        /* 设置News对象的title */        news.setTitle(buf.toString().trim());        buf.setLength(0);        this.in_title = false;      } else      {        /* 设置RSS的title */        title = buf.toString().trim();        buf.setLength(0);        this.in_mainTitle = false;      }    } else if (localName.equals("link"))    {      if (this.in_item)      {        /* 设置News对象的link */        news.setLink(buf.toString().trim());        buf.setLength(0);        this.in_link = false;      }    } else if (localName.equals("description"))    {      if (in_item)      {        /* 设置News对象的description */        news.setDesc(buf.toString().trim());        buf.setLength(0);        this.in_desc = false;      }    } else if (localName.equals("pubDate"))    {      if (in_item)      {        /* 设置News对象的pubDate */        news.setDate(buf.toString().trim());        buf.setLength(0);        this.in_date = false;      }    }  }  /* 取得Element的开头结尾中间夹的字符串 */  @Override  public void characters(char ch[], int start, int length)  {    if (this.in_item || this.in_mainTitle)    {      /* 将char[]添加StringBuffer */      buf.append(ch, start, length);    }  }}

    /* 设置mLink为网页连接 */    Linkify.addLinks(mLink, Linkify.WEB_URLS);



0 0
原创粉丝点击