移动互联网开发:SQLite和contentprovider
源码链接:
1.SQLite:https://gitee.com/huo_shui_three_thousand/SQLite
2.contentprovider:
https://gitee.com/huo_shui_three_thousand/MyWeChat
1.SQLite数据库的DAO标准CURD操作
效果如下:
布局:
新建SQLite数据库打开助手类DbHelper:
public class DbHelper extends SQLiteOpenHelper {
public static final String TB_NAME = "friends"; //表名
//构造方法:第1参数为上下文,第2参数库库名,第3参数为游标工厂,第4参数为版本
public DbHelper(Context context, String dbname, SQLiteDatabase.CursorFactory factory, int version) {
super(context, dbname, factory, version); //创建或打开数据库
}
@Override
public void onCreate(SQLiteDatabase db) {
//当表不存在时,创建表;第一字段为自增长类型
db.execSQL("CREATE TABLE IF NOT EXISTS " +
TB_NAME + "( _id integer primary key autoincrement," +
"name varchar," + "age integer"+ ")");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// 执行SQL命令
db.execSQL("DROP TABLE IF EXISTS " + TB_NAME);
onCreate(db);
}
}
新建数据库访问类MyDAO:
public class MyDAO {
private SQLiteDatabase myDb; //类的成员
private DbHelper dbHelper; //类的成员
public MyDAO(Context context) { //构造方法,参数为上下文对象
//第1参数为上下文,第2参数为数据库名
dbHelper = new DbHelper(context,"test.db",null,1);
}
public Cursor allQuery(){ //查询所有记录
myDb = dbHelper.getReadableDatabase();
return myDb.rawQuery("select * from friends",null);
}
public int getRecordsNumber(){ //返回数据表记录数
myDb = dbHelper.getReadableDatabase();
Cursor cursor= myDb.rawQuery("select * from friends",null);
return cursor.getCount();
}
public void insertInfo(String name,int age){ //插入记录
myDb = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", name);
values.put("age", age);
long rowid=myDb.insert(DbHelper.TB_NAME, null, values);
if(rowid==-1)
Log.i("myDbDemo", "数据插入失败!");
else
Log.i("myDbDemo", "数据插入成功!"+rowid);
}
public void deleteInfo(String selId){ //删除记录
String where = "_id=" + selId;
int i = myDb.delete(DbHelper.TB_NAME, where, null);
if (i > 0)
Log.i("myDbDemo", "数据删除成功!");
else
Log.i("myDbDemo", "数据未删除!");
}
public void updateInfo(String name,int age,String selId){ //修改记录
//方法中的第三参数用于修改选定的记录
ContentValues values = new ContentValues();
values.put("name", name);
values.put("age", age);
String where="_id="+selId;
int i=myDb.update(DbHelper.TB_NAME, values, where, null);
//上面几行代码的功能可以用下面的一行代码实现
//myDb.execSQL("update friends set name = ? ,age = ? where _id = ?",new Object[]{name,age,selId});
if(i>0)
Log.i("myDbDemo","数据更新成功!");
else
Log.i("myDbDemo","数据未更新!");
}
}
2.完善WeChat项目的“通讯录模块”
之前框架模块构建:
-
https://blog.csdn.net/weixin_45710656/article/details/105007036
-
https://blog.csdn.net/weixin_45710656/article/details/105177105
效果如下:
新增和改动布局:
新建ContactAdapter适配器:
public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ContactsViewHolder> {
private List<ContactDataBean> contactsAccounts;
private Context mContext;
public ContactAdapter(Context context, List<ContactDataBean> contactsAccounts) {
mContext=context;
this.contactsAccounts = contactsAccounts;
}
@Override
public ContactsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.contact_item, parent, false);
return new ContactsViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ContactsViewHolder holder, int position) {
holder.tv_Name2.setText(contactsAccounts.get(position).getName());
holder.tv_Phone2.setText(contactsAccounts.get(position).getPhoneNumber());
}
@Override
public int getItemCount() {
return contactsAccounts.size();
}
class ContactsViewHolder extends RecyclerView.ViewHolder {
LinearLayout LLName,LLPhone;
TextView tv_Name1, tv_Name2,tv_Phone1,tv_Phone2;
public ContactsViewHolder(@NonNull View itemView) {
super(itemView);
LLName=itemView.findViewById(R.id.LLName);
LLPhone=itemView.findViewById(R.id.LLPhone);
tv_Name1=itemView.findViewById(R.id.tv_Name1);
tv_Name2=itemView.findViewById(R.id.tv_Name2);
tv_Phone1=itemView.findViewById(R.id.tv_Phone1);
tv_Phone2=itemView.findViewById(R.id.tv_Phone2);
}
}
}
contactFragment的一些改动:
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this.getActivity(),new String[]{Manifest.permission.READ_CONTACTS},1);
}
view= inflater.inflate(R.layout.tab08, container, false);
return view;
}