PullRefreshLibrary

package com.bawei.map;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import com.bawei.map.bean.Yuekao;
import com.bawei.map.bean.Yuekao.Result.Rows;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.format.DateUtils;
import android.widget.ListView;
import android.widget.Toast;

public class NextActivity extends Activity {

    private MyAdapter adapter;
    private ArrayList<Rows> rows;
    Handler handler=new Handler(){
        

        

        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
            case 0:
                String str=(String) msg.obj;
                Gson gson=new Gson();
                Yuekao yuekao = gson.fromJson(str, Yuekao.class);
                rows = yuekao.result.rows;
                adapter = new MyAdapter(NextActivity.this,rows);
                refreshableView.setAdapter(adapter);
                
                break;

            default:
                break;
            }
        };
    };
    private PullToRefreshListView next_listView;
    private ListView refreshableView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_next);


==========================================================================

        
        next_listView=(PullToRefreshListView)findViewById(R.id.next_listView);                       刷新
        refreshableView = next_listView.getRefreshableView();
        
        =======================================================================
        
        final String path="http://api.fang.anjuke.com/m/android/1.3/shouye/recInfosV3/?city_id=14&lat=40.04652&lng=116.306033&api_key=androidkey&sig=9317e9634b5fbc16078ab07abb6661c5&macid=45cd2478331b184ff0e15f29aaa89e3e&app=a-ajk&_pid=11738&o=PE-TL10-user+4.4.2+HuaweiPE-TL10+CHNC00B260+ota-rel-keys%2Crelease-keys&from=mobile&m=Android-PE-TL10&cv=9.5.1&cid=14&i=864601026706713&v=4.4.2&qtime=20160411091603&pm=b61&uuid=1848c59c-185d-48d9-b0e9-782016041109&_chat_id=0";
        //加载数据
        getData(path);

       

====================================================================================

        next_listView.setOnRefreshListener(new OnRefreshListener<ListView>() {

            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                rows.clear();
                getData(path);
                Toast.makeText(NextActivity.this, "正在刷新", 0).show();
                DateUtils.formatDateTime(NextActivity.this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
                
                adapter.notifyDataSetChanged();
                
                new Handler().postDelayed(new Runnable() {
                    
                    @Override
                    public void run() {
                        next_listView.onRefreshComplete();                                                      刷新
                        
                    }
                },0);
                
            }
        });
        
        
        next_listView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {

            @Override
            public void onLastItemVisible() {
                getData(path);
                Toast.makeText(NextActivity.this, "正在加载", 0).show();
                adapter.notifyDataSetChanged();
                
            }
        });
        

    }

==============================================================================


    private void getData(final String path) {
        new Thread(){
            public void run() {
                try {
                    URL url=new URL(path);
                    HttpURLConnection openConnection = (HttpURLConnection) url.openConnection();
                    openConnection.setConnectTimeout(5000);
                    openConnection.setReadTimeout(5000);
                    int responseCode = openConnection.getResponseCode();
                    if(responseCode==200){
                        InputStream inputStream = openConnection.getInputStream();
                        String str = StreamToStr(inputStream);
                        Message msg = Message.obtain();
                        msg.obj=str;
                        msg.what=0;
                        handler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            };
        }.start();
        
        
    }

    public String StreamToStr(InputStream inputStream){
        try {
            ByteArrayOutputStream arrayOutputStream=new ByteArrayOutputStream();
            int len=0;
            byte[] buffer=new byte[1024];
            while((len=inputStream.read(buffer))!=-1){
                arrayOutputStream.write(buffer, 0, len);
            }
            return arrayOutputStream.toString();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
        
    }
    
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值