原创XListView

本文介绍了一个使用Android平台进行网络数据加载的应用实例。通过XListView实现下拉刷新和上拉加载更多功能,并利用HttpUtils发起GET请求获取JSON数据,解析为自定义的数据结构并展示在列表中。
摘要由CSDN通过智能技术生成
自己写的
package com.example.lianxi;

import java.io.IOException;
import java.util.LinkedList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.com.cctest.view.XListView;
import org.com.cctest.view.XListView.IXListViewListener;

import com.bwie.beantuijian.Data;
import com.bwie.beantuijian.Data.DataBean;
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.util.Log;

public class MainActivity extends Activity implements IXListViewListener {
	String path = "http://ic.snssdk.com/2/article/v25/stream/?category=news_entertainment&count=20&min_behot_time=1465266126&bd_latitude=4.9E-324&bd_longitude=4.9E-324&bd_loc_time=1465265107&loc_mode=5&lac=4527&cid=28883&iid=4464422609&device_id=17258390367&ac=wifi&channel=baidu&aid=13&app_name=news_article&version_code=460&device_platform=android&device_type=SAMSUNG&os_api=19&os_version=4.4.4&uuid=353040417908000&openudid=84c1c7b192991cc6";
	private XListView listView;
	private Handler handler = new Handler() {
		private Data data;
		private LinkedList<DataBean> list;
		LinkedList<DataBean> listbean;
		private MyAdapter myAdapter;

		public void handleMessage(Message msg) {
			data = (Data) msg.obj;
			switch (msg.arg1) {
			case 0:
				list = data.getData();
				listbean = list;
				myAdapter = new MyAdapter(listbean, MainActivity.this);
				listView.setAdapter(myAdapter);
				stopListView();
				break;
			case 1:
				list.clear();
				list = data.getData();
				for (DataBean d : list) {
					listbean.addFirst(d);

				}
				myAdapter.notifyDataSetChanged();
				stopListView();
				break;
			case 2:
				list.clear();
				list = data.getData();
				for (DataBean d : list) {
					listbean.addLast(d);
				}
				myAdapter.notifyDataSetChanged();
				stopListView();
				break;
			default:
				break;
			}

		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		listView = (XListView) findViewById(R.id.xlistview);
		listView.setPullLoadEnable(true);
		listView.setPullRefreshEnable(true);
		listView.setXListViewListener(this);
		httpJson(path, 0);

	}

	public void httpJson(String path, final int i) {
		HttpUtils utils = new HttpUtils();
		utils.configCurrentHttpCacheExpiry(0);
		utils.send(HttpMethod.GET, path, new RequestCallBack<String>() {
			// 获取失败
			@Override
			public void onFailure(HttpException arg0, String arg1) {
				// TODO Auto-generated method stub

			}

			// 获取成功
			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				// TODO Auto-generated method stub
				String result = arg0.result;
				Gson gson = new Gson();
				Data data = gson.fromJson(result, Data.class);
				Message message = new Message();
				message.obj = data;
				message.arg1 = i;
				handler.sendMessage(message);
			}
		});
	}

	/**
	 * 停止刷新,下拉
	 */
	public void stopListView() {

		listView.stopLoadMore();
		listView.stopRefresh();
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		httpJson(path, 1);
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		httpJson(path, 2);
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值