xml pull 解析 XlistView 上拉加载 下拉刷新


注意网络权限配置  



主页面代码展示

package com.example.xmlpull;

import java.util.ArrayList;
import java.util.List;

import me.maxwin.view.XListView;
import me.maxwin.view.XListView.IXListViewListener;


import utils.HttpUtils;

import bean.News;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {
    private List<News> list;
    private List<News> listt=new ArrayList<News>();
    private String url="http://www.sciencenet.cn/xml/iphoneInterface.aspx?type=news&nums=20&pass=";
    private XListView xlv;
    private MyAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        xlv=(XListView)findViewById(R.id.xlv);
        
        
        //开启下拉刷新
        xlv.setPullRefreshEnable(true);
        //开启上拉加载
        xlv.setPullLoadEnable(true);
        new AsyncTask<String, Void, List<News>>(){

            @Override
            protected List<News> doInBackground(String... params) {
                try {
                    String s = HttpUtils.qudu(params[0]);
                    Log.i("999", s);
                    list=HttpUtils.paerss(s);
                    Log.i("0000", list.size()+"");
                    return list;
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return list;
            }

            protected void onPostExecute(java.util.List<News> result) {
                adapter=new MyAdapter(MainActivity.this,result);
                xlv.setAdapter(adapter);
                xlv.setXListViewListener(new IXListViewListener() {
                    
                    @Override
                    public void onRefresh() {
                        // TODO Auto-generated method stub
                        listt.clear();
                        shuju();
                        adapter.notifyDataSetChanged();
                        stopLoadorRefresh();
                    }
                    
                    

                    @Override
                    public void onLoadMore() {
                        // TODO Auto-generated method stub
                        
                        shuju1();
                        adapter.notifyDataSetChanged();
                        stopLoadorRefresh();
                    }
                });
            };
            
        }.execute(url);
        
    }
    private void stopLoadorRefresh(){
        xlv.stopLoadMore();
        xlv.stopRefresh();
        xlv.setRefreshTime("刚刚");
    }
    private void shuju() {
        new Thread(){
            public void run() {
                try {
                    
                    String s = HttpUtils.qudu(url);
                    Log.i("999", s);
                    list=HttpUtils.paerss(s);
                    Log.i("0000", list.size()+"");
                    adapter=new MyAdapter(MainActivity.this,list);
                    xlv.setAdapter(adapter);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            };
        }.start();
    }
    private void shuju1() {
        new Thread(){
            public void run() {
                try {
                    
                    String s = HttpUtils.qudu(url);
                    Log.i("999", s);
                    list=HttpUtils.paerss(s);
                    Log.i("0000", list.size()+"");
                    Log.i("tjl", listt.size()+"");    
                    listt.addAll(list);
                    Log.i("tjl", listt.size()+"ttt");
                    adapter=new MyAdapter(MainActivity.this,listt);
                    xlv.setAdapter(adapter);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            };
        }.start();
    }
    
}

xml解析页面代码展示


package utils;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import bean.News;

import android.content.res.XmlResourceParser;
import android.util.Xml;


public class HttpUtils {
        public static String qudu(String path) throws Exception{
            //建立一个URL对象  
            URL url = new URL(path);  
            //得到打开的链接对象  
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
            //设置请求超时与请求方式  
            conn.setReadTimeout(5*1000);  
            conn.setRequestMethod("GET");  
            //从链接中获取一个输入流对象  
            BufferedReader bf=new BufferedReader(new InputStreamReader(conn.getInputStream()));
            StringBuffer sb=new StringBuffer();
            String ss;
            while((ss=bf.readLine())!=null){
                sb.append(ss);
            }
            return sb.toString();
        }
        public static List<News> paerss(String str) throws Exception{
            List<News> list=new ArrayList<News>();
            XmlPullParser pull=Xml.newPullParser();
            ByteArrayInputStream in=new ByteArrayInputStream(str.getBytes());
            pull.setInput(in,"utf-8");
            News n=null;
            int type = pull.getEventType();
            while(type!=XmlResourceParser.END_DOCUMENT){
                String name=pull.getName();
                switch (type) {
                case XmlResourceParser.START_DOCUMENT:
                    break;
                case XmlResourceParser.START_TAG:
                    if("item".equalsIgnoreCase(name)){
                        n=new News();
                    }else if("title".equalsIgnoreCase(name)){
                        n.setTitle(pull.nextText());
                    }else if("link".equalsIgnoreCase(name)){
                        n.setLink(pull.nextText());
                    }else if("copyright".equalsIgnoreCase(name)){
                        n.setCopyright(pull.nextText());
                    }else if("pubDate".equalsIgnoreCase(name)){
                        n.setPubDate(pull.nextText());
                    }
                    break;
                case XmlResourceParser.END_TAG:
                    if("item".equalsIgnoreCase(name)){
                        list.add(n);
                        n=null;
                    }
                    break;
                default:
                    break;
                }
                type=pull.next();
            }
            return list;
        }
                    
}    

适配器页面


package com.example.xmlpull;

import java.util.List;

import bean.News;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter implements ListAdapter {
    private Context context;
    private List<News> list;
    public MyAdapter(Context context, List<News> list) {
        super();
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list!=null?list.size():0;
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        holder ho;
        if(convertView==null){
            convertView=View.inflate(context, R.layout.item, null);
            ho=new holder();
            ho.tv_title=(TextView)convertView.findViewById(R.id.tv_title);
            ho.tv_time=(TextView)convertView.findViewById(R.id.tv_time);
            ho.tv_copyright=(TextView)convertView.findViewById(R.id.tv_copyright);
            convertView.setTag(ho);
        }else{
            ho=(holder) convertView.getTag();
        }
        ho.tv_title.setText(list.get(position).getTitle());
        ho.tv_time.setText(list.get(position).getPubDate());
        ho.tv_copyright.setText(list.get(position).getCopyright());
        return convertView;
    }
    class holder{
        TextView tv_title;
        TextView tv_time;
        TextView tv_copyright;
    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值