0

我试图在自定义列表视图中显示解析的 xml 数据,但屏幕似乎是空的。我正在尝试在类对象中添加 xml 数据并将其显示在自定义列表视图中。但是我的类变量没有得到赋值

列出 myNews = new ArrayList();

    try{
        URL url = new URL("http://timesofindia.feedsportal.com/c/33039/f/533916/index.rss");

        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(false);
        XmlPullParser xpp = factory.newPullParser();

         xpp.setInput(getInputStream(url), "UTF_8"); 

         boolean insideItem = false;

         int eventType = xpp.getEventType();

         while(eventType != XmlPullParser.END_DOCUMENT){

             if(eventType == XmlPullParser.START_TAG){

                 if(xpp.getName().equalsIgnoreCase("item")){
                     insideItem = true;
                 }
                 else if(xpp.getName().equalsIgnoreCase("title")){
                     if(insideItem){
                         headLines = xpp.nextText().toString();
                     }
                 }
                 else if(xpp.getName().equalsIgnoreCase("link")){
                     if(insideItem){
                         links.add(xpp.nextText());

                     }
                 }
                 else if(xpp.getName().equalsIgnoreCase("pubDate")){
                     if(insideItem){
                         date = xpp.nextText().toString();

                     }
                 }


             }
             else if(eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){
                 insideItem = false;
             }

             myNews.add(new News(headLines, date));
             eventType = xpp.next();
         }
    }
    catch (Exception e) {
        Log.d("Exception" , e.toString());
    }

    LazyAdapter adapter = new LazyAdapter(this, myNews);
   setListAdapter(adapter);
    ListView listView = getListView();
    listView.setTextFilterEnabled(true);

这是我的 LazyAdapter 代码...

公共类 LazyAdapter 扩展 BaseAdapter {

 Activity activity;
 private List<News> myNews;


public LazyAdapter(Activity a, List<News> myNews) {
    this.activity = a;
    this.myNews = myNews;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    News news = myNews.get(position);
    View v = convertView;

    if(v == null){
        LayoutInflater inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.list, null);
    }

    TextView heading = (TextView)v.findViewById(R.id.textView1);
    heading.setText(news.getTitle());
    TextView pubDate = (TextView)v.findViewById(R.id.textView2);
    pubDate.setText(news.getPubDate());
    heading.setText(news.getTitle());
    pubDate.setText(news.getPubDate());

    return v;
}

@Override
public int getCount() {

    return myNews.size();
}

@Override
public Object getItem(int position) {

    return myNews.get(position);
}

@Override
public long getItemId(int position) {

    return position;
}

}

4

1 回答 1

0

首先,我建议使用Spring Framework for Android来处理您的 http 请求解析您的 XML。该框架以非常干净的方式处理 XML 和 JSON 解析。

另外,你能发布你的LazyAdapter代码吗?

于 2012-09-12T12:55:01.157 回答