您的位置:首页 > 理论基础 > 计算机网络

[置顶] XListView上拉刷新下拉加载(网络请求json)

2016-06-01 11:35 429 查看
XML布局

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: