package com.baidu.farment;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.client.ClientProtocolException;
import com.baidu.net.Network;
import com.baidu.vo.Mynews;
import com.baidu.vo.Mynewslist;
import com.baidu.vo.Super;
import com.example.xlistviews.XListView;
import com.example.xlistviews.XListView.IXListViewListener;
import com.example.yuekao1.Base;
import com.example.yuekao1.R;
import com.thoughtworks.xstream.XStream;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
public class F1 extends Fragment implements IXListViewListener {
private XListView lv;
private String URL;
public F1(String URL) {
this.URL = URL;
}
ArrayList<Mynews> li = new ArrayList<Mynews>();
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.f1, null);
lv = (XListView) view.findViewById(R.id.lv);
lv.setPullLoadEnable(true);
lv.setPullRefreshEnable(true);
lv.setXListViewListener(this);
new Asy().execute();
return view;
}
class Asy extends AsyncTask<String, Integer, String> {
private String ss1;
private ArrayList<Mynews> news;
@Override
protected String doInBackground(String... params) {
try {
ss1 = Network.getjson(URL);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ss1;
}
@Override
protected void onPostExecute(String result) {
// t1.setText(result);
XStream xs = new XStream();
xs.processAnnotations(Super.class);
Super ss = (Super) xs.fromXML(result);
news = ss.getNewslist().getNews();
li = news;
Base b = new Base(news, getActivity());
lv.setAdapter(b);
}
}
class Asynt extends AsyncTask<String, Integer, String> {
private String ss1;
private ArrayList<Mynews> news;
@Override
protected String doInBackground(String... params) {
try {
ss1 = Network.getjson(URL);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ss1;
}
@Override
protected void onPostExecute(String result) {
// t1.setText(result);
XStream xs = new XStream();
xs.processAnnotations(Super.class);
Super ss = (Super) xs.fromXML(result);
news = ss.getNewslist().getNews();
li.addAll(news);
Base b = new Base(news, getActivity());
b.notifyDataSetInvalidated();
}
}
public void onRefresh() {
new Asy().execute();
onLoad();
}
public void onLoadMore() {
new Asynt().execute();
onLoad();
}
private void onLoad() {
lv.stopRefresh();
lv.stopLoadMore();
lv.setRefreshTime("2016-4-1");
}
}