一些高级代码1

该博客介绍了如何在Android应用中使用XListView组件实现下拉刷新和上拉加载功能。通过创建一个MainActivity类,设置XListView的监听器,并在后台线程中获取网络数据,更新ListView的内容。文章展示了使用HttpURLConnection请求数据,Gson库解析JSON,以及适配器更新数据的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.bwie.xlistview;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import org.apache.http.protocol.HTTP;


import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;


import com.bwie.bean.Bean;
import com.bwie.bean.Data;
import com.bwie.xlistview.view.XListView;
import com.bwie.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;


public class MainActivity extends Activity implements IXListViewListener {


private XListView xListView;
int i = 1;
List<Data> list = new ArrayList<Data>();
Handler handler = new Handler() {
private MyAdapter myAdapter;


@Override
public void handleMessage(Message msg) {
super.handleMessage(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.data);
// 刷新适配器
myAdapter.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.data);
// 刷新适配器
myAdapter.notifyDataSetChanged();
// 停止上拉加载
xListView.stopLoadMore();
break;
case 3:
// 第一次进入要有数据
list.addAll(bean.data);
myAdapter = new MyAdapter(list, MainActivity.this);
xListView.setAdapter(myAdapter);
break;


default:
break;
}
}
};
private String path;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xListView = (XListView) findViewById(R.id.xlistview);
// 开启加载更多
xListView.setPullLoadEnable(true);
xListView.setPullRefreshEnable(true);
new Thread(){
public void run() {

getdata(3);
};
}.start();


// 设置监听
xListView.setXListViewListener(this);


}


// 上拉刷新
@Override
public void onRefresh() {
new Thread() {
public void run() {


getdata(1);
};
}.start();


}


// 下拉加载更多
@Override
public void onLoadMore() {
new Thread(){
public void run() {

getdata(2);
};
}.start();


}


// 请求网络数据
private void getdata(final int tag) {


if (tag == 1) {
path = "http://api.expoon.com/AppNews/getNewsList/type/3/p/1";
} else if (tag == 2) {
path = "http://api.expoon.com/AppNews/getNewsList/type/3/p/"+(i + 1);
} else if (tag == 3) {
path = "http://api.expoon.com/AppNews/getNewsList/type/3/p/1";
}


try {
URL url = new URL(path);
// 得到HttpURLConnection对象
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
// 读取超时
connection.setReadTimeout(5000);
// 请求超时
connection.setConnectTimeout(5000);
// 请求方式
connection.setRequestMethod("GET");
// 连接
connection.connect();
if (200 == connection.getResponseCode()) {
InputStream inputStream = connection.getInputStream();


BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream));


StringBuilder builder = new StringBuilder();
String s;
while ((s = reader.readLine()) != null) {
// 记录每一行数据
builder.append(s);


}
String string = builder.toString();
Message msg = Message.obtain();
msg.obj = string;
msg.arg1 = tag;
handler.sendMessage(msg);
}


} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值