我试图在自定义列表视图中显示解析的 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;
}
}