关闭

httputils及xlistview刷新

43人阅读 评论(0) 收藏 举报
public class MainActivity extends Activity implements IXListViewListener {
	private XListView xListView;
	private List<Item> list;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		xListView=(XListView) findViewById(R.id.listView);
		xListView.setPullRefreshEnable(true);
	    xListView.setPullLoadEnable(true);
	    xListView.setXListViewListener(this);
	    getHttp(1);
	}
	protected void getHttp(int cid) {
//		http://apis.juhe.cn/cook/index?key=3503cb5ddd606f6ab42ead52878007b8&cid=1&dtype=xml
		// TODO Auto-generated method stub
		String url="http://apis.juhe.cn/cook/index";
		HttpUtils httpUtils=new HttpUtils();
		RequestParams params=new RequestParams();
	    params.addBodyParameter("key","3503cb5ddd606f6ab42ead52878007b8");
	    params.addBodyParameter("cid", cid+"");
	    params.addBodyParameter("dtype","xml");
	    httpUtils.send(HttpMethod.POST, url, params, new RequestCallBack<String>() {
			@Override
			public void onFailure(HttpException arg0, String arg1) {
				// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this, "网络连接失败啦", 0).show();
			}
			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				// TODO Auto-generated method stub
				XStream xStream=new XStream();
				xStream.processAnnotations(Root.class);
				Root fromXml=(Root) xStream.fromXML(arg0.result);
				list=fromXml.getResult().getData().getItem();
				Log.i("Tag", list.toString());
				Myadapter adapter=new Myadapter(getApplicationContext(), list);
				xListView.setAdapter(adapter);
			}
		});
	}
	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		getHttp(1);
		onLoad();
	}
	private void onLoad() {
		// TODO Auto-generated method stub
		 	xListView.stopRefresh();
	        xListView.stopLoadMore();
	        // 设置日期格式
	        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        // 获取当前系统时间
	        String nowTime = df.format(new Date(System.currentTimeMillis()));
	        // 释放时提示正在刷新时的当前时间
	        xListView.setRefreshTime(nowTime);
	}
	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		getHttp(1);
		onLoad();
	}
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:213次
    • 积分:8
    • 等级:
    • 排名:千里之外
    • 原创:0篇
    • 转载:4篇
    • 译文:0篇
    • 评论:0条
    文章存档