1. Create a database
public class MHelp extends SQLiteOpenHelper {
//Create a database
public MHelp(Context context) {
super(context, "jh.db", null, 1);
}
//Create a data table
@Override
public void onCreate(SQLiteDatabase db ) {
//db.execSQL("create table jh_user (id Integer primary key autoincrement, date text,thumbnail_pic_s text,thumbnail_pic_s02 text,thumbnail_pic_s03 text ,title text)");
db.execSQL("create table jh_user (id Integer primary key) autoincrement, author_name text, date text,thumbnail_pic_s text,thumbnail_pic_s02 text,thumbnail_pic_s03 text ,title text)"); public class UserDao{ private final SQLiteDatabase database; public UserDao(Context context) { //Create database
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
2.Create Dao
MHelp mHelp = new MHelp(context);
database = mHelp.getReadableDatabase();
}
//Add
public void insert(String author_name, String date, String thumbnail_pic_s, String thumbnail_pic_s02, String thumbnail_pic_s03, String title) {
database.execSQL("insert into jh_user (author_name,date,thumbnail_pic_s,thumbnail_pic_s02,thumbnail_pic_s03, title) values(?,?,?,?,?,?)",new Object[]{author_name,date,thumbnail_pic_s,thumbnail_pic_s02,thumbnail_pic_s02,thumbnail,title_s03); List<ResponBean.DataItem> list = new ArrayList<>(); while (cursor.moveToNext()){ String author_name = cursor.getString(cursor.getColumnIndex("author_name")); String date = cursor.getString(cursor. getColumnIndex("date"));
}
//Query
public List<ResponBean.DataItem> query() {
Cursor cursor = database.rawQuery("select * from jh_user", null );
String thumbnail_pic_s = cursor.getString(cursor.getColumnIndex("thumbnail_pic_s")); list.add(dataItem); } return list; } }//This is a method in the interface @Override public void doString(List<ResponBean.DataItem > dataItems) { if(dataItems == null){ Toast.makeText(getActivity(), "Data request error", Toast.LENGTH_SHORT).show(); return; } updateData(dataItems); loadComplete(); //to Add data to the database for(int i=0; i<dataItems.size();i++){
String thumbnail_pic_s02 = cursor.getString(cursor.getColumnIndex("thumbnail_pic_s02"));
String thumbnail_pic_s03 = cursor.getString(cursor .getColumnIndex("thumbnail_pic_s03"));
String title = cursor.getString(cursor.getColumnIndex("title"));
ResponBean.DataItem dataItem = new ResponBean.DataItem(author_name, date,thumbnail_pic_s, thumbnail_pic_s02, thumbnail_pic_s03, title);
String author_name = dataItems.get(i).getAuthor_name();
String date = dataItems.get(i).getDate();
String thumbnail_pic_s = dataItems.get(i).getThumbnail_pic_s();
String thumbnail_pic_s02 = dataItems .get(i).getThumbnail_pic_s02();
String thumbnail_pic_s03 = dataItems.get(i).getThumbnail_pic_s03();
String title = dataItems.get(i).getTitle();
dao.insert(author_name,date,thumbnail_pic_s,thumbnail_pic_s02,thumbnail_pic_s03,title);
}
}
3. Database and display
//Determine whether there is a network, usually written at the beginning of the program
if(!NetWork.isNetwork(getActivity())){
Toast.makeText(getActivity(), "Network is not available", Toast.LENGTH_SHORT).show( );
//Jump settings interface
//startActivity(new Intent(Settings.ACTION_SETTINGS));
//Create a thread to query the database and assign it
thread();
}else{
}
return view;
}
//Query the database and display
private void thread() {
new Thread(new Runnable() {
@Override
public void run() {
List<ResponBean.DataItem> query = dao.query();
adapter.setData(query);
}
}){}.start();
}
4. The utility class used
public class NetWork {
/**
* Determine whether there is a network
* */
public static boolean isNetwork(Context context){
//Get system service, connect service
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
//Get active network Information
NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo();
//If the available network is not empty and available, it means there is a network
return activeNetworkInfo != null && activeNetworkInfo.isAvailable();
}
}
---------------------
Author: Ka Fai, feed
Source: CSDN
Original: blog.csdn.net/jiahui6666/...
Copyright: This article is a blogger original article , Please attach a link to the blog post for reprinting!
public class MHelp extends SQLiteOpenHelper {
//Create a database
public MHelp(Context context) {
super(context, "jh.db", null, 1);
}
//Create a data table
@Override
public void onCreate(SQLiteDatabase db ) {
//db.execSQL("create table jh_user (id Integer primary key autoincrement, date text,thumbnail_pic_s text,thumbnail_pic_s02 text,thumbnail_pic_s03 text ,title text)");
db.execSQL("create table jh_user (id Integer primary key) autoincrement, author_name text, date text,thumbnail_pic_s text,thumbnail_pic_s02 text,thumbnail_pic_s03 text ,title text)"); public class UserDao{ private final SQLiteDatabase database; public UserDao(Context context) { //Create database
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
2.Create Dao
MHelp mHelp = new MHelp(context);
database = mHelp.getReadableDatabase();
}
//Add
public void insert(String author_name, String date, String thumbnail_pic_s, String thumbnail_pic_s02, String thumbnail_pic_s03, String title) {
database.execSQL("insert into jh_user (author_name,date,thumbnail_pic_s,thumbnail_pic_s02,thumbnail_pic_s03, title) values(?,?,?,?,?,?)",new Object[]{author_name,date,thumbnail_pic_s,thumbnail_pic_s02,thumbnail_pic_s02,thumbnail,title_s03); List<ResponBean.DataItem> list = new ArrayList<>(); while (cursor.moveToNext()){ String author_name = cursor.getString(cursor.getColumnIndex("author_name")); String date = cursor.getString(cursor. getColumnIndex("date"));
}
//Query
public List<ResponBean.DataItem> query() {
Cursor cursor = database.rawQuery("select * from jh_user", null );
String thumbnail_pic_s = cursor.getString(cursor.getColumnIndex("thumbnail_pic_s")); list.add(dataItem); } return list; } }//This is a method in the interface @Override public void doString(List<ResponBean.DataItem > dataItems) { if(dataItems == null){ Toast.makeText(getActivity(), "Data request error", Toast.LENGTH_SHORT).show(); return; } updateData(dataItems); loadComplete(); //to Add data to the database for(int i=0; i<dataItems.size();i++){
String thumbnail_pic_s02 = cursor.getString(cursor.getColumnIndex("thumbnail_pic_s02"));
String thumbnail_pic_s03 = cursor.getString(cursor .getColumnIndex("thumbnail_pic_s03"));
String title = cursor.getString(cursor.getColumnIndex("title"));
ResponBean.DataItem dataItem = new ResponBean.DataItem(author_name, date,thumbnail_pic_s, thumbnail_pic_s02, thumbnail_pic_s03, title);
String author_name = dataItems.get(i).getAuthor_name();
String date = dataItems.get(i).getDate();
String thumbnail_pic_s = dataItems.get(i).getThumbnail_pic_s();
String thumbnail_pic_s02 = dataItems .get(i).getThumbnail_pic_s02();
String thumbnail_pic_s03 = dataItems.get(i).getThumbnail_pic_s03();
String title = dataItems.get(i).getTitle();
dao.insert(author_name,date,thumbnail_pic_s,thumbnail_pic_s02,thumbnail_pic_s03,title);
}
}
3. Database and display
//Determine whether there is a network, usually written at the beginning of the program
if(!NetWork.isNetwork(getActivity())){
Toast.makeText(getActivity(), "Network is not available", Toast.LENGTH_SHORT).show( );
//Jump settings interface
//startActivity(new Intent(Settings.ACTION_SETTINGS));
//Create a thread to query the database and assign it
thread();
}else{
}
return view;
}
//Query the database and display
private void thread() {
new Thread(new Runnable() {
@Override
public void run() {
List<ResponBean.DataItem> query = dao.query();
adapter.setData(query);
}
}){}.start();
}
4. The utility class used
public class NetWork {
/**
* Determine whether there is a network
* */
public static boolean isNetwork(Context context){
//Get system service, connect service
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
//Get active network Information
NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo();
//If the available network is not empty and available, it means there is a network
return activeNetworkInfo != null && activeNetworkInfo.isAvailable();
}
}
---------------------
Author: Ka Fai, feed
Source: CSDN
Original: blog.csdn.net/jiahui6666/...
Copyright: This article is a blogger original article , Please attach a link to the blog post for reprinting!