[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);
}
});
}
}
by: 发表于:2017-07-13 11:00:44 顶(0) | 踩(0) 回复
??
回复评论