DataBaseHelper
package view.zhengxiaolong.bw.com.myholdertext;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
public class DataBaseHelper extends SQLiteOpenHelper {
private Context mContent;
public DataBaseHelper(Context context) {
super(context, "User.db", null, 1);
mContent = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
//建表
String sql = "create table user(image text,title text)";
db.execSQL(sql);
Toast.makeText(mContent, "Create succeeded", Toast.LENGTH_SHORT).show();
Log.i("0000000","Create succeeded");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
UserDao
package view.zhengxiaolong.bw.com.myholdertext;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
/**
* Created by lenovo on 2018/4/1.
*/
public class UserDao {
private DataBaseHelper sqlist;
public UserDao(Context context) {
sqlist = new DataBaseHelper(context);
}
public void add(String image,String title){
SQLiteDatabase database = sqlist.getReadableDatabase();
ContentValues values = new ContentValues();
values.put("image", image);
values.put("title", title);
database.insert("user", null, values);
}
//删除
// public void delete(String title){
// database.delete("user","title =?",new String[]{title});
// }
//修改
// public void updata(String oldTilte,String newImage,String newTitle){
// ContentValues values = new ContentValues();
// values.put("image",newImage);
// values.put("title",newTitle);
// database.update("user",values,"title=?",new String[]{oldTilte});
// }
//查询
public List<User> select(){
List<User> list = new ArrayList<>();
SQLiteDatabase readableDatabase = sqlist.getReadableDatabase();
Cursor cursor = readableDatabase.query("user", null, null, null, null, null, null);
while (cursor.moveToNext()){
String image = cursor.getString(cursor.getColumnIndex("image"));
String title = cursor.getString(cursor.getColumnIndex("title"));
list.add(new User(image,title));
}
return list;
}
// private DataBaseHelper helper;
//
// public UserDao(Context context) {
// helper = new DataBaseHelper(context);
// }
//
// //添加
// public void add(String imgs,String title){
// SQLiteDatabase readableDatabase = helper.getReadableDatabase();
// String sql = "insert into user(id,image,title) value(null,?,?)";
//
// readableDatabase.execSQL(sql,new String[]{imgs, title});
//
// readableDatabase.close();
//
// }
//
// //查询
// public List<User> select(){
// List<User> list = new ArrayList<>();
// SQLiteDatabase readableDatabase = helper.getReadableDatabase();
// String sql = "select * from user";
// Cursor cursor = readableDatabase.rawQuery(sql, new String[]{});
// while (cursor.moveToNext()){
// String image = cursor.getString(cursor.getColumnIndex("image"));
// String title = cursor.getString(cursor.getColumnIndex("title"));
// User user = new User(image,title);
// list.add(user);
//
// }
// return list;
// }
//
// //删除
// public void delete(String title){
// SQLiteDatabase readableDatabase = helper.getReadableDatabase();
// String sql = "delete from user where title=?";
// readableDatabase.execSQL(sql,new String[]{title});
// readableDatabase.close();
// }
//
// //修改
// public void update(String tj,String newimage,String newtitle){
// SQLiteDatabase readableDatabase = helper.getReadableDatabase();
// String sql = "update user set image=?,title=? where title=?";
// readableDatabase.execSQL(sql,new String[]{tj,newimage,newtitle});
// readableDatabase.close();
// }
}
网络判断
YesOrNo
package view.zhengxiaolong.bw.com.myholdertext;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
/**
* Created by lenovo on 2018/3/31.
*/
public class YesOrNo {
public static boolean isNetWork(Context context){
ConnectivityManager manager=(ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
if (manager==null){
return false;
}
NetworkInfo info = manager.getActiveNetworkInfo();
if (info==null||!info.isAvailable()){
return false;
}
return true;
}
}