listview下拉刷新
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"
>
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/pull_refresh_list"
/>
</RelativeLayout>
MainActivity
// 找控件
listView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
// 请求数据
new GetDataTask()
.execute("数据地址");
----------
// listview刷新监听
listView.setOnRefreshListener(new OnRefreshListener<ListView>() {
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
String label = DateUtils.formatDateTime(
getApplicationContext(), System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE
| DateUtils.FORMAT_ABBREV_ALL);
refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
// 再次请求数据
new GetDataTask().execute("地址");
}
});
----------
//异步操作
private class GetDataTask extends AsyncTask<String, integer, String> {
private String str;
@Override//运行在子线程做耗时操作
protected String doInBackground(String... params) {
String string = params[0];
HttpClient httpClient = new DefaultHttpClient();
try {
HttpResponse response = httpClient
.execute(new HttpPost(string));
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
str = EntityUtils.toString(entity);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}
@Override//运行在主线程
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.i("Main", result);
Gson gson = new Gson();
MyBean myBean = gson.fromJson(result, MyBean.class);
list = myBean.data;
mAdapter = new MyBaseAdapter(MainActivity.this, list);
listView.setAdapter(mAdapter);
//每次请求完数据通知适配器数据改变
mAdapter.notifyDataSetChanged();
//通知listview刷新完成
listView.onRefreshComplete();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
