public class MainActivity extends Activity {
// 成员变量
private List<Bean> list = new ArrayList<Bean>();
private List<Bean> two = new ArrayList<Bean>();
private Bean bean;
private XListView xlv;
private MyAdapter adapter;
private int j = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
into("刷新");
xlv = (XListView) findViewById(R.id.lv);
// 激活加载更多
xlv.setPullLoadEnable(true);
// 激活下拉刷新
xlv.setPullRefreshEnable(true);
// 设置xlistView上拉加载下拉刷新的监听
xlv.setXListViewListener(new IXListViewListener() {
@Override
// 下拉刷新的回调方法
public void onRefresh() {
j += 1;
// 下拉刷新,应该展示最新数据,所以清空集合
into("刷新");
// 设置刷新时间
xlv.setRefreshTime(dateFormat());
// 停止加载更多
xlv.stopLoadMore();
// 停止刷新
xlv.stopRefresh();
}
@Override
// //上拉加载的回调方法
public void onLoadMore() {
j += 1;
into("加载");
// 停止加载更多
xlv.stopLoadMore();
// 停止刷新
xlv.stopRefresh();
}
});
}
//加载时间
protected String dateFormat() {
long currentTimeMillis = System.currentTimeMillis();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String format = dateFormat.format(currentTimeMillis);
return format;
}
private void into(final String num) {
final String path = "http://www.oschina.net/action/api/tweet_list?Uid=0&pageIndex=" + j + "&pageSize=5";
Toast.makeText(this, ""+j, 0).show();
new Thread() {
public void run() {
HttpURLConnection con;
try {
con = (HttpURLConnection) new URL(path).openConnection();
con.setConnectTimeout(15000);
con.setRequestMethod("GET");
int i = con.getResponseCode();
if (i == 200) {
InputStream input = con.getInputStream();
XmlPullParser parser = Xml.newPullParser();
parser.setInput(input, "UTF-8");
int event = parser.getEventType();
String str = null;
// XmlPullParser.END_DOCUMENT文档结束
while (event != XmlPullParser.END_DOCUMENT) {
switch (event) {
case XmlPullParser.START_DOCUMENT:
break;
// XmlPullParser.START_TAG节点
case XmlPullParser.START_TAG:
str = parser.getName();
if ("tweet".equals(str)) {
bean = new Bean();
}
break;
// XmlPullParser.TEXT内容
case XmlPullParser.TEXT:
if ("id".equals(str)) {
bean.setId(parser.getText().trim());
}
if ("portrait".equals(str)) {
bean.setPortrait(parser.getText().trim());
}
if ("author".equals(str)) {
bean.setAuthor(parser.getText().trim());
}
if ("authorid".equals(str)) {
bean.setAuthorid(parser.getText().trim());
}
if ("body".equals(str)) {
bean.setBody(parser.getText().trim());
}
if ("pubDate".equals(str)) {
bean.setPubDate(parser.getText().trim());
}
break;
case XmlPullParser.END_TAG:
str = parser.getName();
if ("tweet".equals(str)) {
if (num.equals("刷新")) {
list.add(bean);
two.addAll(0, list);
} else {
two.addAll(list);
}
// 更新UI
runOnUiThread(new Runnable() {
@Override
public void run() {
intoviewp();
}
});
bean = null;
}
str = "";
break;
}
event = parser.next();
}
}
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
}
//适配器
private void intoviewp() {
if (adapter == null) {
adapter = new MyAdapter(two, getApplicationContext());
xlv.setAdapter(adapter);
} else {
adapter.notifyDataSetChanged();
}
}
}