package com.example.myapplication3.Myku;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyHelper extends SQLiteOpenHelper {
public MyHelper(Context context) {
super(context, "MySqls,db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table users1(id integer primary key autoincrement,title text,thumbnail_pic_s text,thumbnail_pic_s02 text, thumbnail_pic_s03 text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
DAO
package com.example.myapplication3.Myku;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.example.myapplication3.Bean;
import java.util.ArrayList;
import java.util.List;
public class Dao {
private final SQLiteDatabase db;
public Dao(Context context){
MyHelper helper = new MyHelper(context);
db = helper.getWritableDatabase();
}
public List<Bean.DataBean> select(){
Cursor query = db.query("users1", null, null, null, null, null, null);
List<Bean.DataBean> list= new ArrayList<>();
while (query.moveToNext()){
String title = query.getString(query.getColumnIndex("title"));
String thumbnail_pic_s = query.getString(query.getColumnIndex("thumbnail_pic_s"));
String thumbnail_pic_s02 = query.getString(query.getColumnIndex("thumbnail_pic_s02"));
String thumbnail_pic_s03 = query.getString(query.getColumnIndex("thumbnail_pic_s03"));
list.add(new Bean.DataBean(title,thumbnail_pic_s,thumbnail_pic_s02,thumbnail_pic_s03));
}
return list;
}
public void add(String title,String thumbnail_pic_s,String thumbnail_pic_s02,String thumbnail_pic_s03){
ContentValues values=new ContentValues();
values.put("title",title);
values.put("thumbnail_pic_s",thumbnail_pic_s);
values.put("thumbnail_pic_s02",thumbnail_pic_s02);
values.put("thumbnail_pic_s03",thumbnail_pic_s03);
db.insert("users1",null,values);
}
public void delect(){
db.execSQL("DELETE FROM users1");
}
}
package com.example.luck.myapplication.tab_fragments;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import com.example.luck.myapplication.Beans.Bean;
import com.example.luck.myapplication.R;
import com.example.luck.myapplication.Sqlite.Dao;
import com.example.luck.myapplication.utils.BaseFragment;
import com.example.luck.myapplication.utils.MyAdapter_list;
import com.example.luck.myapplication.utils.NetUtils;
import com.example.pulltolibrary.PullToRefreshBase;
import com.example.pulltolibrary.PullToRefreshListView;
import java.util.ArrayList;
import java.util.List;
/**
* @Auther: luck
* @Date: 2019/1/11 14:11:37
* @Description:
*/
public class tab_Fragment01 extends BaseFragment {
private PullToRefreshListView plistview;
private int num=1;
private String path="http://www.xieast.com/api/news/news.php?page=";
private Dao dao;
private List<Bean.DataBean> list=new ArrayList<>();
private MyAdapter_list myAdapter_list;
@Override
protected void initData() {
plistview.setMode(PullToRefreshBase.Mode.BOTH);
myAdapter_list = new MyAdapter_list(getActivity(), list);
plistview.setAdapter(myAdapter_list);
dao = new Dao(getActivity());
if (NetUtils.getInstance().panduan(getActivity())==false){
Toast.makeText(getActivity(),"没网了",Toast.LENGTH_SHORT).show();
list.addAll(dao.select());
plistview.onRefreshComplete();
return;
}else if (NetUtils.getInstance().panduan(getActivity())==true){
getData(1);
}
initLisnner();
}
private void initLisnner() {
plistview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
num=1;
list.clear();
getData(1);
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
num++;
getData(num);
}
});
}
private void getData(int num) {
NetUtils.getInstance().ResqultData1(path + num, Bean.class, new NetUtils.CallBack<Bean>() {
@Override
public void onseuues(Bean bean) {
//将请求出来的数据设置给集合
list.addAll(bean.getData());
//为了避免数据库重复添加
dao.delect();
//循环添加到数据库
for (int i=0;i<bean.getData().size();i++){
dao.add(bean.getData().get(i).getAuthor_name(),bean.getData().get(i).getThumbnail_pic_s());
}
//将list重新复制
list.addAll(dao.select());
//刷新适配器
myAdapter_list.notifyDataSetChanged();
}
});
}
@Override
protected void initView(View view) {
plistview = view.findViewById(R.id.plistview);
}
@Override
protected int getLayoutId() {
return R.layout.tab_fragment01;
}
}
//判断有网无网
if (NetUtils.getinstance().panduan(getActivity())==false){
Toast.makeText(getActivity(),"没网了",Toast.LENGTH_SHORT).show();
//没网的时候查询适配器
list.addAll(dao.select());
}else {
//有网的时候获取数据并添加到数据库
NetUtils.getinstance().getRequltData(Api + num, Bean.class, new NetUtils.CallBack<Bean>() {
@Override
public void onsuess(Bean bean) {
list.addAll(bean.getData());
//为了避免重复添加 清空数据库
dao.delect();
//将数据添加到数据库-*
for (int i=0;i<bean.getData().size();i++){
dao.add(bean.getData().get(i).getNews_title(),bean.getData().get(i).getPic_url());
}
list.addAll(dao.select());
myAdapter.notifyDataSetChanged();
}
});