<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/plv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.handmark.pulltorefresh.library.PullToRefreshListView>
MainActivity
//添加依赖
//compile ‘com.github.userswlwork:pull-to-refresh:1.0.0’
package com.lll.sqlite_pulltorefresh;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
String baseUrl = “http://www.93.gov.cn/93app/data.do?channelId=0&startNum=0”;
ArrayList list = new ArrayList<>();
private PullToRefreshListView plv;
int page = 0;
private Dao dao;
private ArrayAdapter mAdapter;
private String mUrl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找控件
plv = findViewById(R.id.plv);
//设置上下拉都支持
plv.setMode(PullToRefreshBase.Mode.BOTH);
//设置Adapter
mAdapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,list);
plv.setAdapter(mAdapter);
//创建Dao层
dao = new Dao(MainActivity.this);
//请求网络
getNetData(page);
//上下拉监听
plv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
list.clear();
getNetData(0);
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
page++;
getNetData(page);
}
});
}
private void getNetData(int page) {
mUrl = baseUrl + page;
if (NetStateUtil.isConn(this)){//这个是网络获取的逻辑
new MAsyTask().execute(mUrl);
}else {
Toast.makeText(this,"当前没有网络,请稍后重试",Toast.LENGTH_SHORT).show();
//从数据库查询
String s = dao.queryData(mUrl);
if (!s.isEmpty()){
Log.d("lv","从数据库中取数据");
//解析 显示
Gson gson = new Gson();
Product product = gson.fromJson(s, Product.class);
List<Product.DataBean> data = product.getData();
//初始化一个空的字符串
ArrayList<String> stringlist = new ArrayList<>();
for (int i = 0;i<data.size();i++){
stringlist.add(data.get(i).getTITLE());
}
list.addAll(stringlist);
mAdapter.notifyDataSetChanged();
//让刷新头部和底部返回
plv.onRefreshComplete();
}
}
}
private class MAsyTask extends AsyncTask<String,Void,String> {
@Override
protected String doInBackground(String... strings) {
return Net.getJson(strings[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//调用插入的方法
dao.insertData(mUrl,s);
Gson gson = new Gson();
Product product = gson.fromJson(s, Product.class);
List<Product.DataBean> data = product.getData();
//初始化一个空的字符串
ArrayList<String> stringlist = new ArrayList<>();
for (int i = 0;i<data.size();i++){
stringlist.add(data.get(i).getTITLE());
}
list.addAll(stringlist);
mAdapter.notifyDataSetChanged();
//让刷新头部和底部返回
plv.onRefreshComplete();
}
}
}
网络请求页面
package com.lll.sqlite_pulltorefresh;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
class NetStateUtil {
/**
* 判断网络连接是否已经开
* true已经打开 false未打开
* @param context
* @return
*/
public static boolean isConn(Context context) {
boolean bisConnFlag = false;
//上下文获取网络信息
ConnectivityManager conManager = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
//加权限 获取网络信息
NetworkInfo network = conManager.getActiveNetworkInfo();
//判断网络信息是否可用
if (network!=null){
bisConnFlag = conManager.getActiveNetworkInfo().isAvailable();
}
return bisConnFlag;
}
}
MySqlite页面
package com.lll.sqlite_pulltorefresh;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MySqlite extends SQLiteOpenHelper {
public MySqlite( Context context) {
super(context, “user.db”, null, 1);
}
/**
* FROMNAME : 光明日报
* ID : 31719783
* IMAGEURL : http://www.93.gov.cn/11002/upload/webcms/content/image/2018/10/27/08_37_01_334_63185_6y28rdyzfhljdqnklsmckc.jpg
* RN : 1
* SHOWTIME : 2018-10-27
* SUBTITLE : ——访九三学社中央主席武维华
* TITLE : 要有志向做“顶天立地”的研究
*/
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE user(personid INTEGER PRIMARY KEY AUTOINCREMENT," +
"url VARCHAR(20)," +
"json VARCHAR(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
Dao层
package com.lll.sqlite_pulltorefresh;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
class Dao {
private final SQLiteDatabase db;
public Dao(Context context) {
//创建MySqlite
MySqlite mySqlite = new MySqlite(context);
db = mySqlite.getWritableDatabase();
}
//写方法
//插入数据
public void insertData(String url,String jsonstr){
//先删除,再插入
db.delete(“user”,“url=?”,new String[]{jsonstr});
ContentValues values = new ContentValues();
values.put("url",url);
values.put("json",jsonstr);
long user = db.insert("user", null, values);
Log.d("zzz","insert----user"+user);
}
/**
* 查询数据
*/
public String queryData(String url){
String json = "";
Cursor cursor = db.query("user", null, "url=?", new String[]{url}, null, null, null);
while (cursor.moveToNext()){
json = cursor.getString(cursor.getColumnIndex("json"));
}
return json;
}
}