用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

刷新

2016-12-19 作者: 张贤伟举报

[java]代码库

package com.example.day09_xlistview;
 
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import com.example.day09_xlistview.bean.Bean;
import com.example.day09_xlistview.bean.Info;
import com.example.day09_xlistview.view.XListView;
import com.example.day09_xlistview.view.XListView.IXListViewListener;
import com.google.gson.Gson;
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.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
 
public class MainActivity extends Activity implements IXListViewListener {
    String[] arr = new String[] { "1", "2", "3", "4", "5",
 
    };
    // String path =
    // "http://v.juhe.cn/weixin/query?key=b3edac6e41f3f3c84b855019e47b7ace&pno="
    // + 1;
 
    int i = 1;
    private XListView xListView;
    List<Info> list = new ArrayList<>();
    private MyBaseAdapter myBaseAdapter;
    Handler handler = new Handler() {
 
        public void handleMessage(android.os.Message msg) {
 
            String s = (String) msg.obj;
 
            Gson gson = new Gson();
            Bean bean = gson.fromJson(s, Bean.class);
 
            int tag = msg.arg1;
 
            switch (tag) {
            // 下拉刷新
            case 1:
                // 清空之前的数据
                list.clear();
                // 添加新数据
                list.addAll(bean.result.list);
                myBaseAdapter.notifyDataSetChanged();
                // 刷新适配器
                // 停止刷新
                xListView.stopRefresh();
             
                //格式化毫秒值
                SimpleDateFormat dateFormat=new SimpleDateFormat("hh:mm:ss");
                 
                String format = dateFormat.format(new Date());
                 
                xListView.setRefreshTime(format);
                 
                break;
 
            // 上拉加载
            case 2:
                // 追加新数据
                list.addAll(bean.result.list);
                myBaseAdapter.notifyDataSetChanged();
                // 停止加载更多
                xListView.stopLoadMore();
                break;
            // 加载第一次的数据
            case 3:
 
                list.addAll(bean.result.list);
 
                myBaseAdapter = new MyBaseAdapter(list,MainActivity.this);
 
                xListView.setAdapter(myBaseAdapter);
 
                break;
            }
 
        };
 
    };
 
    private ArrayAdapter<String> arrayAdapter;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        xListView = (XListView) findViewById(R.id.xListView);
        // 开启加载更多
        xListView.setPullLoadEnable(true);
        // 设置适配器前就要有数据
        getData(3);
 
        // 设置xListView上拉,下拉监听
        xListView.setXListViewListener(this);
 
    }
 
    @Override
    // xListView下拉刷新监听方法
    public void onRefresh() {
        // 联网得到新数据
 
        getData(1);
         
 
    }
 
    @Override
    // xListView上拉加载更多监听方
    public void onLoadMore() {
        getData(2);
         
    }
 
    /**
     * 请求网络数据
     */
    private void getData(final int tag) {
 
        String path = null;
        // 是刷新调用的联网请求
        if (tag == 1) {
 
            path = "http://v.juhe.cn/weixin/query?key=b3edac6e41f3f3c84b855019e47b7ace&pno=" + 1;
            // 是加载更多调用的联网请求
        } else if (tag == 2) {
 
            path = "http://v.juhe.cn/weixin/query?key=b3edac6e41f3f3c84b855019e47b7ace&pno="
                    + (i += 1);
            // 加载第一次的数据
        } else if (tag == 3) {
            path = "http://v.juhe.cn/weixin/query?key=b3edac6e41f3f3c84b855019e47b7ace&pno=" + 1;
 
        }
 
        HttpUtils httpUtils = new HttpUtils();
 
        httpUtils.send(HttpMethod.GET, path, new RequestCallBack<String>() {
 
            @Override
            public void onFailure(HttpException arg0, String arg1) {
 
            }
 
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String result = arg0.result;
 
                // 把响应信息发送给handler
                Message msg = Message.obtain();
 
                msg.obj = result;
                msg.arg1 = tag;
 
                handler.sendMessage(msg);
 
            }
        });
    }
}


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...