package com.bwei.Test.zhangjian20170808xlist.Activity;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import com.bwei.Test.zhangjian20170808xlist.Adapter.MyAdapter;
import com.bwei.Test.zhangjian20170808xlist.Bean.MyBean;
import com.bwei.Test.zhangjian20170808xlist.Bean.MyData;
import com.bwei.Test.zhangjian20170808xlist.R;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.TextHttpResponseHandler;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import cz.msebera.android.httpclient.Header;
import test.bwei.com.xlistlibrary.XListView;
public class MainActivity extends AppCompatActivity {
private XListView listView;
private Handler handler=new Handler();
private int page=1;
private List<MyData> list;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView= (XListView) findViewById(R.id.lv);
listView.setPullLoadEnable(true);
listView.setPullRefreshEnable(true);
listView.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {
Toast.makeText(MainActivity.this,"刷新",Toast.LENGTH_SHORT).show();
getTime();
page = 1;
getData();
handler.postDelayed(new Runnable() {
@Override
public void run() {
listView.stopRefresh();
}
},2000);
}
@Override
public void onLoadMore() {
Toast.makeText(MainActivity.this,"加载",Toast.LENGTH_SHORT).show();
handler.postDelayed(new Runnable() {
@Override
public void run() {
page++;
getData();
listView.stopLoadMore();
}
},2000);
}
});
//获取数据
getData();
}
private void getTime() {
long millis = System.currentTimeMillis();
Date date=new Date(millis);
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = format.format(date);
listView.setRefreshTime(time);
}
public void getData() {
String path="http://www.yulin520.com/a2a/forum/allTypeList?sign=376C5BFC22179A1B8FF3A86D4588B29F&pageSize=10&ts=1877785007&forumType=0&page="+page;
AsyncHttpClient asyncHttpClient=new AsyncHttpClient();
asyncHttpClient.get(this, path, new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
Gson gson=new Gson();
MyBean bean = gson.fromJson(responseString, MyBean.class);
if (page==1){
list = new ArrayList<MyData>();
}
for (int i=0;i<bean.getData().size();i++){
list.add(bean.getData().get(i));
}
initData(list);
}
});
}
private void initData(List<MyData> list) {
if (adapter == null) {
//第一次创建
adapter = new MyAdapter(this, list);
listView.setAdapter(adapter);
} else {
//适配器不为空 直接刷新
adapter.notifyDataSetChanged();
}
}
}
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import com.bwei.Test.zhangjian20170808xlist.Adapter.MyAdapter;
import com.bwei.Test.zhangjian20170808xlist.Bean.MyBean;
import com.bwei.Test.zhangjian20170808xlist.Bean.MyData;
import com.bwei.Test.zhangjian20170808xlist.R;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.TextHttpResponseHandler;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import cz.msebera.android.httpclient.Header;
import test.bwei.com.xlistlibrary.XListView;
public class MainActivity extends AppCompatActivity {
private XListView listView;
private Handler handler=new Handler();
private int page=1;
private List<MyData> list;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView= (XListView) findViewById(R.id.lv);
listView.setPullLoadEnable(true);
listView.setPullRefreshEnable(true);
listView.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {
Toast.makeText(MainActivity.this,"刷新",Toast.LENGTH_SHORT).show();
getTime();
page = 1;
getData();
handler.postDelayed(new Runnable() {
@Override
public void run() {
listView.stopRefresh();
}
},2000);
}
@Override
public void onLoadMore() {
Toast.makeText(MainActivity.this,"加载",Toast.LENGTH_SHORT).show();
handler.postDelayed(new Runnable() {
@Override
public void run() {
page++;
getData();
listView.stopLoadMore();
}
},2000);
}
});
//获取数据
getData();
}
private void getTime() {
long millis = System.currentTimeMillis();
Date date=new Date(millis);
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = format.format(date);
listView.setRefreshTime(time);
}
public void getData() {
String path="http://www.yulin520.com/a2a/forum/allTypeList?sign=376C5BFC22179A1B8FF3A86D4588B29F&pageSize=10&ts=1877785007&forumType=0&page="+page;
AsyncHttpClient asyncHttpClient=new AsyncHttpClient();
asyncHttpClient.get(this, path, new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
Gson gson=new Gson();
MyBean bean = gson.fromJson(responseString, MyBean.class);
if (page==1){
list = new ArrayList<MyData>();
}
for (int i=0;i<bean.getData().size();i++){
list.add(bean.getData().get(i));
}
initData(list);
}
});
}
private void initData(List<MyData> list) {
if (adapter == null) {
//第一次创建
adapter = new MyAdapter(this, list);
listView.setAdapter(adapter);
} else {
//适配器不为空 直接刷新
adapter.notifyDataSetChanged();
}
}
}