[置顶] XListView上拉刷新下拉加载(网络请求json)
2016-06-01 11:35
429 查看
XML布局
activity_main.xml
Xin实体类
MainActivity中
MyAdapter适配器
values文件中 strings
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <com.example.utils.XListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent" > </com.example.utils.XListView> </RelativeLayout>
Xin实体类
package com.example.shua; import java.util.ArrayList; public class Xin { public String reason; public ArrayList<RR> result; public class RR { public String address; } }
MainActivity中
package com.example.shua; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import com.example.shua.Xin.RR; import com.example.utils.SimpleDataExample; import com.example.utils.XListView; import com.example.utils.XListView.IXListViewListener; import com.google.gson.Gson; public class MainActivity extends Activity implements IXListViewListener { List<RR> al = new ArrayList<RR>(); private MyAdapter adapter; int page = 1; private String path; private Xin fromJson; private XListView xlistView; Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case 1: fromJson = (Xin) msg.obj; adapter = new MyAdapter(MainActivity.this); xlistView.setAdapter(adapter); adapter.addrest(al); break; default: break; } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); xlistView = (XListView) findViewById(R.id.listView); // 添加XListView的上拉和下拉刷新监听器 xlistView.setPullLoadEnable(true); xlistView.setPullRefreshEnable(true); xlistView.setXListViewListener(this); getsubmit(); } private void getsubmit() { new Thread() { public void run() { try { path = "http://apis.haoservice.com/lifeservice/travel/scenery?pid=2&cid=45&page=" + page + "&key=5ca4d84913684265948925e25468b409"; // 定义一个Http客户端对象 HttpClient httpClient = new DefaultHttpClient(); // 定义一个get请求的对象 HttpGet httpGet = new HttpGet(path); // 执行get请求,获取响应 HttpResponse httpResponse = httpClient.execute(httpGet); // 先获取状态行,再获取响应码 int statusCode = httpResponse.getStatusLine() .getStatusCode(); if (statusCode == 200) { // 请求成功 // 获取实体对象 HttpEntity entity = httpResponse.getEntity(); // 获取实体内容流 InputStream inputStream = entity.getContent(); String str = steamToStr(inputStream); Gson gson = new Gson(); Xin fromJson = gson.fromJson(str, Xin.class); Log.i("MainActivity", fromJson.result.get(0).address); al.addAll(fromJson.result); Message obtain = Message.obtain(); obtain.obj = fromJson; obtain.what = 1; handler.sendMessage(obtain); } } catch (Exception e) { e.printStackTrace(); } }; }.start(); } private String steamToStr(InputStream inputStream) throws IOException { ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = inputStream.read(buffer)) != -1) { arrayOutputStream.write(buffer, 0, len); } String content = arrayOutputStream.toString(); return content; } //刷新 @Override public void onRefresh() { al.clear(); page++; getsubmit(); adapter.addrest(al); SimpleDataExample.setFormat("dddddddddddd", getApplicationContext()); SimpleDataExample.getFormat("dddddddddddd", getApplicationContext(), xlistView); } //加载 @Override public void onLoadMore() { page++; getsubmit(); adapter.addrest(al); SimpleDataExample.setFormat("dddddddddddd", getApplicationContext()); SimpleDataExample.getFormat("dddddddddddd", getApplicationContext(), xlistView); }; }
MyAdapter适配器
package com.example.shua; import java.util.ArrayList; import java.util.List; import com.example.shua.Xin.RR; import android.content.Context; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class MyAdapter extends BaseAdapter { Context context; Xin fromJson; List<RR> al = new ArrayList<RR>(); public MyAdapter(Context context) { super(); this.context = context; //this.fromJson=fromJson; } public void addrest(List<RR> al) { this.al.clear(); this.al.addAll(al); this.notifyDataSetChanged(); } @Override public int getCount() { // TODO Auto-generated method stub Log.i("adapter", al.size()+""); return al.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = View.inflate(context, R.layout.items, null); TextView text = (TextView) view.findViewById(R.id.text); text.setText(al.get(position).address); // Log.i("adapter", al.get(position).result.get(position).address); return view; } }
values文件中 strings
<!--上拉刷新下拉加载控件的各个状态--> <string name="xlistview_header_hint_normal">下拉刷新</string> <string name="xlistview_header_hint_ready">松开刷新数据</string> <string name="xlistview_header_hint_loading">正在加载...</string> <string name="xlistview_header_last_time">上次更新时间:</string> <string name="xlistview_footer_hint_normal">查看更多</string> <string name="xlistview_footer_hint_ready">松开载入更多</string> <!--保存到本地的字段--> <string name="sp_name">egg_sp</string>
相关文章推荐
- HTTP遇到的安全问题&&HTTPS解决的问题
- iOS-HTTP 请求方式: GET和POST的比较
- HttpUtils解析xml数据
- Linux几种网络连接模式
- 远程计算机需要网络级别身份验证
- Android 网络连接判断并进行相关处理
- 计算机网络
- MXNet系统上ilsvrc12数据集的制作与inception-bn网络的训练
- Meta标签中的robots标签
- Http请求中Content-Type讲解以及在Spring MVC中的应用
- RAC的GCS和GES 转载自http://blog.csdn.net/csucxcc/archive/2010/09/02/5857373.asp
- BP神经网络(地震数据反推测井曲线)
- ios开发网络篇—HTTP协议
- hdu 3657(最小割求解最大点权独立集)
- HTTP常见状态码介绍
- http accept-encoding详解 HTTP协议-压缩(gzip,deflate)
- 感知器的简单理解
- 网络、分布式等相关资源总结
- cocoapods安装遇到的一些小难题!
- http请求头参数详解