public class Fragment1 extends BaseFragment {
private ListView listView;
private List<JsonBean.DataBean> data;
private JsonBean jsonBean;
private ListView clistView;
private Dao dao;
private Cursor chen;
//布局
@Override
protected int bindLayout() {
return R.layout.fragment1;
}
//控件
@Override
protected void initView() {
dao = new Dao(getActivity());
listView = bindView(R.id.listView);
clistView = bindView(R.id.clistView);
}
//判断有误网
@Override
protected void initData() {
if (HttpUtils.isNetWord(getActivity())) {
HttpUtils.MyAsyncTask myAsyncTask = new HttpUtils.MyAsyncTask();
myAsyncTask.setBacks(new HttpUtils.MyAsyncTask.CallBacks() {
@Override
public void getData(String s) {
Gson gson = new Gson();
jsonBean = gson.fromJson(s, JsonBean.class);
data = jsonBean.getData();
Toast.makeText(getActivity(), "数据请求成功", Toast.LENGTH_SHORT).show();
listView.setAdapter(new MyAdapter(data,getActivity()));
//判断数据库有没有数据
if (dao.select("chen",null,null,null,null,null,null).moveToFirst()){
return;
}else {
for (int i=0;i<data.size();i++){
ContentValues contentValues = new ContentValues();
contentValues.put("news_summary",data.get(i).getNews_summary());
contentValues.put("news_title",data.get(i).getNews_title());
contentValues.put("url",data.get(i).getPic_url());
dao.add("chen",null,contentValues);
}
}
}
});
myAsyncTask.execute("http://api.expoon.com/AppNews/getNewsList/type/1/p/1");
}else {
// Toast.makeText(getActivity(), "无网络连接", Toast.LENGTH_SHORT).show();da
Cursor chen = dao.select("chen", null, null, null, null, null, null);
data= new ArrayList<>();
if (chen.moveToFirst()){
do {
String news_summary = chen.getString(chen.getColumnIndex("news_summary"));
String news_title = chen.getString(chen.getColumnIndex("news_title"));
String url = chen.getString(chen.getColumnIndex("url"));
data.add(new JsonBean.DataBean(news_summary,news_title,url));
}while (chen.moveToNext());
}
listView.setAdapter(new MyAdapter(data,getActivity()));
}
}
//
@Override
protected void bindEvent() {
}
}