xml解析

来源:互联网 发布:高级算法 编辑:程序博客网 时间:2024/06/01 08:59
public class MainActivity extends Activity implements IXListViewListener{

    List<News> allList = new ArrayList<News>();

    int catalog = 1;
    int pageIndex = 1;
    int pageSize = 20;

    private List<News> nList;


    private MyAdapter adapter;

    private XListView lv_main;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        lv_main=(XListView)findViewById(R.id.lv_main);
        adapter = new MyAdapter(this);
        lv_main.setAdapter(adapter);
        
        // 添加XListView的上拉和下拉刷新监听器
        lv_main.setPullLoadEnable(true);
        lv_main.setPullRefreshEnable(true);
        lv_main.setXListViewListener(this);

        // 请求数据
        getData();
        
        lv_main.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
            Intent intent=new Intent(MainActivity.this,NewActivity.class);
            intent.putExtra("body", nList.get(position).body);
            intent.putExtra("pubDate", nList.get(position).pubDate);
            startActivity(intent);
            }
        });
    }

    private void getData() {
        String ulr = "http://www.oschina.net/action/api/news_list?catalog=1&pageIndex="
                + pageIndex + "&pageSize=20";
        Ion.with(getApplicationContext()).load(ulr).asString()
                .setCallback(new FutureCallback<String>() {

                    @Override
                    public void onCompleted(Exception e, String result) {
                        if (e != null) {
                            return;
                        }
                        pull(result);
                        allList.addAll(nList);
                        adapter.addrest(nList);

                    }
                });

    }

    public void pull(String xml) {
        News books = null;
        try {
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            XmlPullParser parser = factory.newPullParser();
            ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(
                    xml.getBytes());
            // 设置流
            parser.setInput(arrayInputStream, "utf-8");

            int eventType = parser.getEventType();
            while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                case XmlPullParser.START_DOCUMENT:

                    nList = new ArrayList<News>();

                    break;
                case XmlPullParser.START_TAG:
                    // 开始解析元素
                    if ("news".equals(parser.getName())) {
                        books = new News();
                    }

                    if (books != null) {
                        if ("id".equals(parser.getName())) {
                            books.id = parser.nextText();
                        } else if ("title".equals(parser.getName())) {
                            books.title = parser.nextText();

                        } else if ("body".equals(parser.getName())) {

                            books.body = parser.nextText();
                        } else if ("commentCount".equals(parser.getName())) {

                            books.commentCount = parser.nextText();
                        } else if ("author".equals(parser.getName())) {

                            books.author = parser.nextText();
                        } else if ("authorid".equals(parser.getName())) {

                            books.authorid = parser.nextText();
                        } else if ("pubDate".equals(parser.getName())) {

                            books.pubDate = parser.nextText();
                        } else if ("newstype".equals(parser.getName())) {

                            if ("type".equals(parser.getName())) {

                                books.newstype.type = parser.nextText();
                            } else if ("authoruid2".equals(parser.getName())) {

                                books.newstype.authoruid2 = parser.nextText();
                            }
                        }
                    }

                    break;
                case XmlPullParser.END_TAG:

                    if("news".equals(parser.getName())){
                        nList.add(books);
                    }

                    break;

                default:
                    break;
                }
                eventType=parser.next();
            }
        } catch (Exception e) {
            
            e.printStackTrace();
        }
    }

    @Override
    public void onRefresh() {
        pageIndex = 1;
        nList.clear();
        allList.clear();
        getData();
        
         SimpleDataExample.setFormat("dddddddddddd", getApplicationContext());
         SimpleDataExample.getFormat("dddddddddddd", getApplicationContext(), lv_main);
        
    }

    @Override
    public void onLoadMore() {
        pageIndex ++;
        getData();
        
         SimpleDataExample.setFormat("dddddddddddd", getApplicationContext());
         SimpleDataExample.getFormat("dddddddddddd", getApplicationContext(), lv_main);
        

    }


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

MyAdapter

public class MyAdapter extends BaseAdapter {
    
    List<News> nList=new ArrayList<News>();

    private Context context;

    public MyAdapter(Context context) {
        this.context=context;
    }

    public void addrest(List<News> nList){
        this.nList.clear();
        this.nList.addAll(nList);
        this.notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return nList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
        
        if(convertView == null){
            convertView = View.inflate(context, R.layout.itme, null);
            viewHolder = new ViewHolder();
            viewHolder.tv_itme_name = (TextView) convertView.findViewById(R.id.tv_itme_name);
            viewHolder.tv_itme_date = (TextView) convertView.findViewById(R.id.tv_itme_date);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
         viewHolder.tv_itme_name.setText(nList.get(position).title);
         viewHolder.tv_itme_date.setText(nList.get(position).pubDate);
        
        return convertView;
    }
    
    class ViewHolder{
        TextView tv_itme_name;
        TextView tv_itme_date;
        
    }


public class News {
    public String id;
    public String commentCount;
    public String authorid;
    public String pubDate;
    public String title;
    public String body;
    public String author;
    public Newstype newstype;

}


public class Newslist {
    public List<News> news;
}


public class Newstype {
    public String type;
    public String authoruid2;
}


public class Oschina {

    public String catalog;
    public String newsCount;
    public String pagesize;
    public List<News> newslist;
}

0 0
原创粉丝点击