XListView
来源:互联网 发布:淘宝巨大猫咪酱假货 编辑:程序博客网 时间:2024/04/24 11:36
package com.example.yuekao1.Fragment;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Xml;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.yuekao1.Blogs;
import com.example.yuekao1.BlogsAdapter;
import com.example.yuekao1.R;
import com.example.yuekao1.utils.XListView;
import com.example.yuekao1.utils.XListView.IXListViewListener;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
public class BoKeUIFragment extends Fragment implements IXListViewListener {
private XListView lv;
private BlogsAdapter adapter;
private int pageIndex = 1;
private ArrayList<Blogs> list = new ArrayList<Blogs>();
private HttpUtils httpUtils;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1, null);
lv = (XListView) view.findViewById(R.id.lv);
lv.setPullLoadEnable(true);
lv.setPullRefreshEnable(true);
lv.setXListViewListener(this);
httpUtils = new HttpUtils();
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String path = "http://www.oschina.net/action/api/blog_list?pageIndex="
+ pageIndex + "&pageSize=10";
reguestData(path);
}
private void reguestData(String path) {
httpUtils.send(HttpMethod.GET, path, new RequestCallBack<String>() {
private Blogs blogs;
ArrayList<Blogs> blogsList;
@Override
public void onFailure(HttpException arg0, String arg1) {
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
try {
String xml = arg0.result;
XmlPullParser newPullParser = Xml.newPullParser();
ByteArrayInputStream bais = new ByteArrayInputStream(xml
.getBytes());
newPullParser.setInput(bais, "utf-8");
int eventType = newPullParser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
String name = newPullParser.getName();
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
blogsList = new ArrayList<Blogs>();
break;
case XmlPullParser.START_TAG:
if ("blog".equals(name)) {
blogs = new Blogs();
} else if ("title".equals(name)) {
blogs.setTitle(newPullParser.nextText());
} else if ("body".equals(name)) {
blogs.setBody(newPullParser.nextText());
} else if ("commentCount".equals(name)) {
blogs.setCommentCount(newPullParser.nextText());
} else if ("authorname".equals(name)) {
blogs.setAuthorname(newPullParser.nextText());
} else if ("pubDate".equals(name)) {
blogs.setPubDate(newPullParser.nextText());
}
break;
case XmlPullParser.END_TAG:
if ("blog".equals(name)) {
blogsList.add(blogs);
blogs = null;
}
break;
}
eventType = newPullParser.next();
}
list.addAll(blogsList);
if (adapter == null) {
adapter = new BlogsAdapter(list, getActivity());
lv.setAdapter(adapter);
} else {
adapter.notifyDataSetChanged();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
@Override
public void onRefresh() {
list.clear();
pageIndex = 1;
String path = "http://www.oschina.net/action/api/blog_list?pageIndex="
+ pageIndex + "&pageSize=10";
reguestData(path);
onLoad();
}
private void onLoad() {
lv.stopRefresh();
lv.stopLoadMore();
lv.setRefreshTime("�ո�");
}
@Override
public void onLoadMore() {
pageIndex += 1;
String path = "http://www.oschina.net/action/api/blog_list?pageIndex="
+ pageIndex + "&pageSize=10";
reguestData(path);
onLoad();
}
@Override
public void onPause() {
adapter = null;
super.onPause();
}
}
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Xml;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.yuekao1.Blogs;
import com.example.yuekao1.BlogsAdapter;
import com.example.yuekao1.R;
import com.example.yuekao1.utils.XListView;
import com.example.yuekao1.utils.XListView.IXListViewListener;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
public class BoKeUIFragment extends Fragment implements IXListViewListener {
private XListView lv;
private BlogsAdapter adapter;
private int pageIndex = 1;
private ArrayList<Blogs> list = new ArrayList<Blogs>();
private HttpUtils httpUtils;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1, null);
lv = (XListView) view.findViewById(R.id.lv);
lv.setPullLoadEnable(true);
lv.setPullRefreshEnable(true);
lv.setXListViewListener(this);
httpUtils = new HttpUtils();
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String path = "http://www.oschina.net/action/api/blog_list?pageIndex="
+ pageIndex + "&pageSize=10";
reguestData(path);
}
private void reguestData(String path) {
httpUtils.send(HttpMethod.GET, path, new RequestCallBack<String>() {
private Blogs blogs;
ArrayList<Blogs> blogsList;
@Override
public void onFailure(HttpException arg0, String arg1) {
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
try {
String xml = arg0.result;
XmlPullParser newPullParser = Xml.newPullParser();
ByteArrayInputStream bais = new ByteArrayInputStream(xml
.getBytes());
newPullParser.setInput(bais, "utf-8");
int eventType = newPullParser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
String name = newPullParser.getName();
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
blogsList = new ArrayList<Blogs>();
break;
case XmlPullParser.START_TAG:
if ("blog".equals(name)) {
blogs = new Blogs();
} else if ("title".equals(name)) {
blogs.setTitle(newPullParser.nextText());
} else if ("body".equals(name)) {
blogs.setBody(newPullParser.nextText());
} else if ("commentCount".equals(name)) {
blogs.setCommentCount(newPullParser.nextText());
} else if ("authorname".equals(name)) {
blogs.setAuthorname(newPullParser.nextText());
} else if ("pubDate".equals(name)) {
blogs.setPubDate(newPullParser.nextText());
}
break;
case XmlPullParser.END_TAG:
if ("blog".equals(name)) {
blogsList.add(blogs);
blogs = null;
}
break;
}
eventType = newPullParser.next();
}
list.addAll(blogsList);
if (adapter == null) {
adapter = new BlogsAdapter(list, getActivity());
lv.setAdapter(adapter);
} else {
adapter.notifyDataSetChanged();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
@Override
public void onRefresh() {
list.clear();
pageIndex = 1;
String path = "http://www.oschina.net/action/api/blog_list?pageIndex="
+ pageIndex + "&pageSize=10";
reguestData(path);
onLoad();
}
private void onLoad() {
lv.stopRefresh();
lv.stopLoadMore();
lv.setRefreshTime("�ո�");
}
@Override
public void onLoadMore() {
pageIndex += 1;
String path = "http://www.oschina.net/action/api/blog_list?pageIndex="
+ pageIndex + "&pageSize=10";
reguestData(path);
onLoad();
}
@Override
public void onPause() {
adapter = null;
super.onPause();
}
}
0 0
- XListView
- XListView
- Xlistview
- XListView
- xListView
- XListView
- xlistview
- xListView
- XlistView
- xlistview
- xlistview
- XListView
- xlistview
- xListview
- XListView
- XListView
- xlistview
- xlistview
- 归一化函数normalize详解
- 3.7 固定基函数的局限性
- CSS3 边框
- 互联网安全与黑客
- Spring AOP 最终版实现
- XListView
- 【web前端】input使用小结
- vs+va助手比较好用的快捷键。
- Java IO - PushbackInputStream&PushbackReader
- 学习第二天
- Sybase 数据库的备份和还原
- LeetCode Excel Sheet Column Title
- PullToRefresh刷新加载
- 导入函数: import和from...import...的区别