pull解析XListView上拉加载下拉刷新

package com.example.yuekao2;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

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

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Xml;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.example.ad.Ad;
import com.example.bean.Bean;
import com.example.util.XListView;
import com.example.util.XListView.IXListViewListener;
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;

public class MainActivity extends Activity {
int index=1;
private XListView lv;
List listall=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
getdata(“http://www.oschina.net/action/api/tweet_list?uid=0&pageIndex=“+index+”&pageSize=5”,1);
}

private void init() {
    lv = (XListView) findViewById(R.id.lv);
    lv.setPullLoadEnable(true);
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            String i=listall.get(position-1).id;
            Intent intent=new Intent(MainActivity.this,Main2Activity.class);
            intent.putExtra("i",i);
            startActivity(intent);
        }
    });
    lv.setXListViewListener(new IXListViewListener() {

        @Override
        public void onRefresh() {
            index=index+1;
            getdata("http://www.oschina.net/action/api/tweet_list?uid=0&pageIndex="+index+"&pageSize=5",2);
        }

        @Override
        public void onLoadMore() {
            index=index+1;
            getdata("http://www.oschina.net/action/api/tweet_list?uid=0&pageIndex="+index+"&pageSize=5",3);
        }
    });
}

Handler h = new Handler() {
    private String name;
    private Bean b;
    private Ad ad;

    public void handleMessage(Message msg) {
        List<Bean> list = new ArrayList<>();
        String s = (String) msg.obj;
        int tag = msg.arg1;
        XmlPullParser xml = Xml.newPullParser();
        ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes());
        try {
            xml.setInput(new InputStreamReader(in));
            int type = xml.getEventType();
            while (type != XmlPullParser.END_DOCUMENT) {
                switch (type) {
                case XmlPullParser.START_TAG:
                    name = xml.getName();
                    if ("tweet".equals(name)) {
                        b = new Bean();
                    }else if ("id".equals(name)) {
                        b.id = xml.nextText();
                    } 
                    else if ("author".equals(name)) {
                        b.author = xml.nextText();
                    } 
                    else if ("portrait".equals(name)) {
                        b.portrait = xml.nextText();
                    } else if ("body".equals(name)) {
                        b.body = xml.nextText();
                    }
                        else if ("imgSmall".equals(name)) {
                            b.imgSmall = xml.nextText();
                        }
                            else if ("imgBig".equals(name)) {
                                b.imgBig = xml.nextText();
                    }
                    break;
                case XmlPullParser.END_TAG:
                    name = xml.getName();
                    if ("tweet".equals(name)) {
                        list.add(b);
                    }
                    break;

                default:
                    break;
                }
                type = xml.next();
            }
            switch (tag) {
            case 1:
                listall.addAll(list);
                ad=new Ad(MainActivity.this, listall);
                lv.setAdapter(ad);
                break;
            case 2:
                listall.clear();
                listall.addAll(list);
                ad.notifyDataSetChanged();
                lv.stopRefresh();
                break;
            case 3:
                listall.addAll(list);
                ad.notifyDataSetChanged();
                lv.stopLoadMore();
                break;

            default:
                break;
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }
    };
};
void getdata(String url, final int tag) {
    HttpUtils util = new HttpUtils();
    util.configCurrentHttpCacheExpiry(0);
    util.send(HttpMethod.GET, url, new RequestCallBack<String>() {

        @Override
        public void onFailure(HttpException arg0, String arg1) {

        }

        @Override
        public void onSuccess(ResponseInfo<String> arg0) {
            String s = arg0.result;
            Message m = Message.obtain();
            m.arg1 = tag;
            m.obj = s;
            h.sendMessage(m);
        }
    });
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值