package com.example.testxml1;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import me.maxwin.view.XListView;
import me.maxwin.view.XListView.IXListViewListener;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import com.example.adapter.MyBAdapter;
import com.example.bean.Bean;
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;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ListView;
import android.R.integer;
import android.app.Activity;
public class MainActivity extends Activity implements IXListViewListener{
private HttpUtils httpUtils;
private List<Bean> lists = new ArrayList<Bean>();
private List<Bean> listss;
private XListView xlistView;
private int start1 = 12;
private Handler handler;
private MyBAdapter adapter;
private static int refereshCnt = 0 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xlistView = (XListView) findViewById(R.id.xlistview);
xlistView.setPullLoadEnable(true);
xlistView.setPullRefreshEnable(true);
xlistView.setXListViewListener(this);
for (int i = 11; i != start1; i++) {
getdata(start1);
}
adapter = new MyBAdapter(lists, MainActivity.this);
xlistView.setAdapter(adapter);
handler = new Handler();
}
private void getdata(int start) {
httpUtils = new HttpUtils();
httpUtils.send(HttpMethod.GET, "http://v.juhe.cn/laohuangli/h?date=2014-09-"+start+"&key=8097aa39feea4ac842b05424e9de355a&dtype=xml", new RequestCallBack<String>() {
@Override
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
// TODO Auto-generated method stub
String uri = arg0.result;
try {
InputStream is = new ByteArrayInputStream(uri.getBytes());
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(is);
Element element = document.getDocumentElement();
NodeList list = element.getElementsByTagName("item");
for (int i = 0; i < list.getLength(); i++) {
Bean b = new Bean();
Element e = (Element) list.item(i);
b.setYangli(e.getElementsByTagName("yangli").item(0).getTextContent());
b.setHours(e.getElementsByTagName("hours").item(0).getTextContent());
b.setDes(e.getElementsByTagName("des").item(0).getTextContent());
b.setYi(e.getElementsByTagName("yi").item(0).getTextContent());
b.setJi(e.getElementsByTagName("ji").item(0).getTextContent());
lists.add(b);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
@Override
public void onRefresh() {
// TODO Auto-generated method stub
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
start1 = ++refereshCnt;
lists.clear();
getdata(start1);
adapter.notifyDataSetChanged();
onLoad();
}
}, 2000);
}
@Override
public void onLoadMore() {
// TODO Auto-generated method stub
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
getdata(start1);
adapter.notifyDataSetChanged();
onLoad();
}
}, 2000);
}
public void onLoad() {
// TODO Auto-generated method stub
xlistView.stopLoadMore();
xlistView.stopRefresh();
xlistView.setRefreshTime("刚刚");
};
}