在Activity中(注意加图片,头布局和脚步局,报错可能出在布局的xlistview包没改)
<span style="font-size:14px;">package com.example.xmltest2; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import org.com.cctest.view.XListView; import org.com.cctest.view.XListView.IXListViewListener; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import com.lidroid.xutils.HttpUtils; import com.lidroid.xutils.exception.HttpException; import com.lidroid.xutils.http.ResponseInfo; import com.lidroid.xutils.http.callback.RequestCallBack; import com.lidroid.xutils.http.client.HttpRequest.HttpMethod; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Xml; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends ActionBarActivity implements IXListViewListener { String path = "http://www.oschina.net/action/api/news_list"; Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { String xml = (String) msg.obj; xml(xml); xlistView.setAdapter(new MyAdapter(MainActivity.this,list)); for (News news : list) { System.out.println(news.toString()); } }; }; LinkedList<News> list = new LinkedList<>(); private News news; private XListView xlistView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); xlistView = (XListView) findViewById(R.id.xlistView); //激活加载更多,默认不是激活的 xlistView.setPullLoadEnable(true); xlistView.setXListViewListener(this); getXml(); } private void getXml() { HttpUtils httpUtils = new HttpUtils(); httpUtils.configCurrentHttpCacheExpiry(0); httpUtils.send(HttpMethod.GET,path,new RequestCallBack<String>() { @Override public void onFailure(HttpException arg0,String arg1) { // TODO Auto-generated method stub } @Override public void onSuccess(ResponseInfo<String> arg0) { String result = arg0.result; Message msg = Message.obtain(); msg.obj = result; handler.sendMessage(msg); } }); } /** * 解析xml * * @param xml */ protected void xml(String xml) { // 得到解析器 XmlPullParser pullParser = Xml.newPullParser(); // 把Stirng转成流 ByteArrayInputStream arrayInputStream = new ByteArrayInputStream( xml.getBytes()); try { pullParser.setInput(new InputStreamReader(arrayInputStream)); // 得到事件类型 int eventType = pullParser.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_TAG: // 得到标签名 String tagName = pullParser.getName(); if ("news".equals(tagName)) { news = new News(); } else if ("title".equals(tagName)) { news.title = pullParser.nextText(); } else if ("pubDate".equals(tagName)) { news.pubDate = pullParser.nextText(); } break; case XmlPullParser.END_TAG: // 结束标签的名字 tagName = pullParser.getName(); if ("news".equals(tagName)) { list.addLast(news); } break; } eventType = pullParser.next(); } } catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override//刷新 public void onRefresh() { getXml(); xlistView.stopLoadMore(); xlistView.stopRefresh(); } @Override//加载更多 public void onLoadMore() { getXml(); xlistView.stopLoadMore(); xlistView.stopRefresh(); } } </span>适配器中
<span style="background-color: rgb(255,255);">package com.example.xmltest2; import java.util.List; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class MyAdapter extends BaseAdapter { Context context; List<News> list; public MyAdapter(Context context,List<News> list) { this.context = context; this.list = list; } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position,View convertView,ViewGroup parent) { View view = View.inflate(context,android.R.layout.simple_list_item_1,null); TextView text1 = (TextView) view.findViewById(android.R.id.text1); text1.setText(list.get(position).title); return view; } }</span><span style="background-color: rgb(255,0);"> </span>
<span style="background-color: rgb(255,0);">Bean包中</span>
<pre name="code" class="java">package com.example.xmltest2; public class News { public String title; public String pubDate; @Override public String toString() { return "News [title=" + title + ",pubDate=" + pubDate + "]"; } }原文链接:https://www.f2er.com/xml/295049.html