边肖将与您分享如何使用Sqlite回收查看对话框来添加、删除和检查数据。希望大家看完这篇文章后有所收获。我们一起讨论一下吧!
原标题要求:
(1)点击“添加联系人”按钮,跳转到“添加”信息对话框,输入用户名、联系电话、地址选择性别,添加后会显示在联系人列表中。
(2)点击联系人中的项目,弹出对话框,修改联系人信息或删除联系人。
(3)您可以通过输入姓名和给出信息来搜索联系信息。
解释
1.这篇博文是代码部分,有一些关于实现的细节。感兴趣的同学可以看看这篇博文。
2.建议阅读《安卓编程权威指南3》,其中Sqlite的代码格式值得学习,或者整本书的代码风格值得学习。
3.我会不断更新作业,注意不要迷路。
翻译
密码
目录结构
这是我的目录结构。适配器包下有适配器,dao包下有访问数据库的接口和实现方法,数据库包下有一些关于数据库的类,对话框包下有自定义对话框,模型包下有数据模型。
添加相关性
因为使用了RecyclerView,所以您必须添加一个依赖项。
实现' com . Android . support : recycle view-v 7:28 . 0 . 0 '
一个
绝对代码
1.User.java
公共类用户{
二等兵UUID身份证;
私有字符串名称;
私人字符串电话;
私有字符串地址;
私人内部性;
公共用户(){ 0
}
公共UUID GetID(){ 0
返回id;
}
公共void SetID(UUID id){ 0
this.id=id
}
公共字符串getName(){ 0
返回名称;
}
公共void setName(字符串名称){ 0
this.name=name
}
公共字符串GetPhone(){ 0
返回电话;
}
公共void setPhone(字符串电话){ 0
this.phone=phone
}
公共字符串GetAddress(){ 0
返回地址;
}
公共void setAddress(字符串地址){ 0
this.address=address
}
public int GetSex(){ 0
回归性;
}
public void SetSex(int sex){ 0
this.sex=sex
}
公共用户(UUID id、字符串名称、字符串电话、字符串地址、内部性别){ 0
this.id = id;
this.name = name;
this.phone = phone;
this.address = address;
this.sex = sex;
}
public User(String name, String phone, String address, int sex) {
this.id =UUID.randomUUID();
this.name = name;
this.phone = phone;
this.address = address;
this.sex = sex;
}
}
2.UserCursorWrapper.java
public class UserCursorWrapper extends CursorWrapper {
/**
* Creates a cursor wrapper.
*
* @param cursor The underlying cursor to wrap.
*/
public UserCursorWrapper(Cursor cursor) {
super(cursor);
}
public User getUser(){
//获取每行数据
String uuidString = getString(getColumnIndex(UserDbSchema.UserTable.Columns.UUID));
String name = getString(getColumnIndex(UserDbSchema.UserTable.Columns.NAME));
String phone = getString(getColumnIndex(UserDbSchema.UserTable.Columns.PHONE));
String address = getString(getColumnIndex(UserDbSchema.UserTable.Columns.ADDRESS));
int sex = getInt(getColumnIndex(UserDbSchema.UserTable.Columns.SEX));
return new User(UUID.fromString(uuidString),name,phone,address,sex);
}
}
3.UserDbSchema.java
public class UserDbSchema {
//利用内部类定义user表结构
public static final class UserTable{
//定义表名
public static final String TABLE_NAME = "user";
//定义数据表字段
public static final class Columns{
public static final String UUID = "uuid";
public static final String NAME = "name";
public static final String PHONE = "phone";
public static final String ADDRESS = "address";
public static final String SEX = "sex";
}
}
}
4.UserSqlHelper.java
public class UserSqlHelper extends SQLiteOpenHelper {
private static final int VERSION = 1;//定义版本
private static final String DATABASE_NAME = "course21DataBase";
public UserSqlHelper(Context context) {
super(context, DATABASE_NAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
/**
此时数据库是没有被创建或者打开的,
直到getReadableDatabase,getWritableDatabase这两个方法其中一个被调用
*/
db.execSQL("create table "+ UserDbSchema.UserTable.TABLE_NAME+
"("+"_id integer primary key autoincrement,"+
UserDbSchema.UserTable.Columns.UUID+","+
UserDbSchema.UserTable.Columns.NAME+","+
UserDbSchema.UserTable.Columns.PHONE+","+
UserDbSchema.UserTable.Columns.ADDRESS+","+
UserDbSchema.UserTable.Columns.SEX+")"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//不更新数据库
}
}
5.IUserDao.java
public interface IUserDao {
/**
* 获取所有用户信息
* */
ArrayList<User> getAllUser();
/**
* 获取指定名字的用户信息,默认名字不重复
* */
User getUserByName(String name);
/**
*添加用户信息
* */
void addUser(User user);
/**
* 修改指定用户信息
* */
void updateUser(User user);
/**
* 删除指定用户信息
* */
void deleteUser(User user);
}
6.UserDao.java
public class UserDao implements IUserDao{
private static UserDao sUserDao;
private Context mContext;
private SQLiteDatabase mDatabase;
public UserDao(Context context) {
mContext = context.getApplicationContext();
mDatabase = new UserSqlHelper(mContext).getWritableDatabase();//此时数据库才是可写的
}
/**
* 全局保留一个userDao实例
* */
public static UserDao getUserDao(Context context){
if (sUserDao == null){
sUserDao = new UserDao(context);
}
return sUserDao;
}
/**
* 获取所有用户信息
*/
@Override
public ArrayList<User> getAllUser() {
ArrayList<User> users = new ArrayList<>();
UserCursorWrapper cursorWrapper = queryUsers(null,null);
try {
cursorWrapper.moveToFirst();
while (!cursorWrapper.isAfterLast()){
users.add(cursorWrapper.getUser());
cursorWrapper.moveToNext();
}
} finally {
cursorWrapper.close();
}
return users;
}
/**
* 获取指定名字的用户信息,默认名字不重复
*
* @param name
*/
@Override
public User getUserByName(String name) {
//定义查询条件
String whereClause = UserDbSchema.UserTable.Columns.NAME + " = ?";
String [] whereArgs = new String[]{ name };
UserCursorWrapper cursorWrapper = queryUsers(whereClause,whereArgs);
try {
//查询失败
if (cursorWrapper.getCount() == 0){
return null;
}
cursorWrapper.moveToFirst();
return cursorWrapper.getUser();
} finally {
//关闭
cursorWrapper.close();
}
}
/**
* 添加用户信息
*
* @param user
*/
@Override
public void addUser(User user) {
//防止传入空值
if (user!=null){
ContentValues values = getContentValues(user);
//插入数据
mDatabase.insert(UserDbSchema.UserTable.TABLE_NAME,null,values);
}
}
/**
* 修改指定用户信息
*
* @param user
*/
@Override
public void updateUser(User user) {
String uuidString = user.getId().toString();
ContentValues values = getContentValues(user);
mDatabase.update(UserDbSchema.UserTable.TABLE_NAME,
values,
UserDbSchema.UserTable.Columns.UUID+" = ? ",
new String[] { uuidString });
}
/**
* 删除指定用户信息
*
* @param user
*/
@Override
public void deleteUser(User user) {
String uuidString = user.getId().toString();
mDatabase.delete(UserDbSchema.UserTable.TABLE_NAME,
UserDbSchema.UserTable.Columns.UUID+" = ? ",
new String[] { uuidString });
}
//私有方法,返回一个ContentValues对象
private ContentValues getContentValues(User user){
ContentValues values = new ContentValues();
//添加键值对
values.put(UserDbSchema.UserTable.Columns.UUID,user.getId().toString());
values.put(UserDbSchema.UserTable.Columns.NAME,user.getName());
values.put(UserDbSchema.UserTable.Columns.PHONE,user.getPhone());
values.put(UserDbSchema.UserTable.Columns.ADDRESS,user.getAddress());
values.put(UserDbSchema.UserTable.Columns.SEX,user.getSex());
return values;
}
/**
* 查询记录,返回一个CursorWrapper对象,可以调用里面的getUser()方法获得User值
*
* 不管是查询特殊还是全部,都可以调用此方法
*/
private UserCursorWrapper queryUsers(String whereClause,String [] whereArgs){
Cursor cursor = mDatabase.query(
UserDbSchema.UserTable.TABLE_NAME,
null,
whereClause,
whereArgs,
null,
null,
null
);
return new UserCursorWrapper(cursor);
}
}
7.UserAdapter.java
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder>{
private ArrayList<User> mUsers;
private Context mContext;//上下文对象
private LayoutInflater mInflater;
private DialogListener mListener = new DialogListener() {
@Override
public void sendMessage() {
updateView();
}
};
public UserAdapter(Context context,ArrayList<User> users) {
mUsers = users;
mContext = context;
mInflater = LayoutInflater.from(mContext);
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.info_item, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder,int position) {
// mPosition = position;
final User user = mUsers.get(position);
//性别决定照片
holder.mImageView.setImageResource(user.getSex()==1?R.drawable.boy:R.drawable.girl);
holder.mTextViewPhone.setText("电话: "+user.getPhone());
holder.mTextViewName.setText(user.getName());
holder.mTextViewAddress.setText("地址: "+user.getAddress());
//点击之后弹出一个对话框
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UpdateDialog dialog = new UpdateDialog(mContext,user,mListener);
dialog.show();
}
});
}
public void updateView(){
mUsers = UserDao.getUserDao(mContext).getAllUser();
notifyDataSetChanged();
}
@Override
public int getItemCount() {
return mUsers.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public ImageView mImageView;
public TextView mTextViewName,mTextViewAddress,mTextViewPhone;
public ViewHolder(@NonNull View itemView) {
super(itemView);
mImageView = itemView.findViewById(R.id.info_image);
mTextViewName = itemView.findViewById(R.id.info_name);
mTextViewAddress = itemView.findViewById(R.id.info_address);
mTextViewPhone = itemView.findViewById(R.id.info_phone);
}
}
}
8.AddDialog.java
public class AddDialog extends Dialog {
private EditText mEditTextName,mEditTextAddress,mEditTextPhone;
private Button mButtonAdd,mButtonCancel;
private RadioGroup mRadioGroup;
private int sex = 1;//性别
private Context mContext;
private DialogListener mListener;
public AddDialog(@NonNull Context context, DialogListener listener) {
super(context);
this.mContext = context;
this.mListener = listener;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_dialog_layout);
initView();
mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.add_radio_sex_boy:
sex = 1;
break;
case R.id.add_radio_sex_girl:
sex = 0;
break;
}
}
});
mButtonCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
mButtonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = mEditTextName.getText().toString();
String address = mEditTextAddress.getText().toString();
String phone = mEditTextPhone.getText().toString();
User user = new User(name,phone,address,sex);
UserDao.getUserDao(mContext).addUser(user);
mListener.sendMessage();
dismiss();
}
});
setCanceledOnTouchOutside(false);
}
//初始化界面
private void initView(){
mEditTextName = findViewById(R.id.edit_add_name);
mEditTextAddress = findViewById(R.id.edit_add_address);
mEditTextPhone = findViewById(R.id.edit_add_phone);
mButtonAdd = findViewById(R.id.button_add_add);
mButtonCancel = findViewById(R.id.button_add_cancel);
mRadioGroup = findViewById(R.id.add_radio_sex);
}
}
9.AddDialog对应布局 add_dialog_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="250dp"
android:layout_height="400dp"
android:background="#FDF5F5">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="80dp">
<TextView
android:layout_centerInParent="true"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加联系人"/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ccc"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:layout_centerInParent="true"
android:background="#E2E7FC"
android:layout_width="180dp"
android:layout_height="200dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:text="姓名:"
android:layout_marginLeft="10dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/edit_add_name"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:textSize="12dp"
android:hint="请输入..."/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:text="性别:"
android:layout_marginLeft="10dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioGroup
android:id="@+id/add_radio_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/add_radio_sex_boy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="男" />
<RadioButton
android:id="@+id/add_radio_sex_girl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:text="电话:"
android:layout_marginLeft="10dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/edit_add_phone"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:textSize="12dp"
android:hint="请输入..."/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:text="地址:"
android:layout_marginLeft="10dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/edit_add_address"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:textSize="12dp"
android:hint="请输入..."/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ccc"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<Button
android:id="@+id/button_add_add"
android:textSize="12dp"
android:layout_width="0dp"
android:layout_gravity="center"
android:layout_weight="3"
android:layout_height="40dp"
android:text="添加"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<Button
android:id="@+id/button_add_cancel"
android:text="取消"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="40dp"
android:layout_gravity="center"
android:textSize="12dp"/>
</LinearLayout>
</LinearLayout>
10. UpdateDialog.java
public class UpdateDialog extends Dialog {
public User mUser;//弹框需要展示的信息
private EditText mEditTextName,mEditTextAddress,mEditTextPhone;
private Button mButtonUpdate,mButtonDelete,mButtonCancel;
private RadioGroup mRadioGroup;
private int sex;
private ImageView mImageView;
private DialogListener mListener;
private Context mContext;
public UpdateDialog(@NonNull Context context,User user, DialogListener listener) {
super(context);
this.mUser = user;
this.mContext = context;
this.mListener = listener;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.update_dialog_layout);
initView();
mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.update_radio_sex_boy:
sex = 1;
break;
case R.id.update_radio_sex_girl:
sex = 0;
break;
}
}
});
//删除按钮操作
mButtonDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UserDao.getUserDao(mContext).deleteUser(mUser);
mListener.sendMessage();
dismiss();
}
});
mButtonCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
mButtonUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// tag = 2;
String name = mEditTextName.getText().toString();
String address = mEditTextAddress.getText().toString();
String phone = mEditTextPhone.getText().toString();
User user = new User(mUser.getId(),name,phone,address,sex);
UserDao.getUserDao(mContext).updateUser(user);
mListener.sendMessage();
dismiss();
}
});
setCanceledOnTouchOutside(false);
}
//初始化界面
private void initView(){
mEditTextName = findViewById(R.id.edit_update_name);
mEditTextAddress = findViewById(R.id.edit_update_address);
mEditTextPhone = findViewById(R.id.edit_update_phone);
mButtonUpdate = findViewById(R.id.button_update_update);
mButtonCancel = findViewById(R.id.button_update_cancel);
mButtonDelete = findViewById(R.id.button_update_delete);
mRadioGroup = findViewById(R.id.update_radio_sex);
mImageView = findViewById(R.id.image_update);
sex = mUser.getSex();
//初始化内容
mRadioGroup.check(mUser.getSex()==1?R.id.update_radio_sex_boy:R.id.update_radio_sex_girl);
mEditTextName.setText(mUser.getName());
mEditTextPhone.setText(mUser.getPhone());
mEditTextAddress.setText(mUser.getAddress());
mImageView.setImageResource(mUser.getSex()==1?R.drawable.boy:R.drawable.girl);
}
}
11. UpdateDialog对应布局 update_dialog_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="250dp"
android:layout_height="400dp"
android:background="#FDF5F5">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="80dp">
<ImageView
android:id="@+id/image_update"
android:src="@drawable/girl"
android:layout_centerInParent="true"
android:layout_width="60dp"
android:layout_height="60dp"/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ccc"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:layout_centerInParent="true"
android:background="#E2E7FC"
android:layout_width="180dp"
android:layout_height="200dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:text="姓名:"
android:layout_marginLeft="10dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/edit_update_name"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:textSize="12dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:text="性别:"
android:layout_marginLeft="10dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioGroup
android:id="@+id/update_radio_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/update_radio_sex_boy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="男" />
<RadioButton
android:id="@+id/update_radio_sex_girl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:text="电话:"
android:layout_marginLeft="10dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/edit_update_phone"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:textSize="12dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:text="地址:"
android:layout_marginLeft="10dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/edit_update_address"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:textSize="12dp"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ccc"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<Button
android:id="@+id/button_update_delete"
android:textSize="12dp"
android:layout_width="0dp"
android:layout_gravity="center"
android:layout_weight="3"
android:layout_height="40dp"
android:text="删除"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<Button
android:id="@+id/button_update_update"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="40dp"
android:layout_gravity="center"
android:text="修改"
android:textSize="12dp"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<Button
android:id="@+id/button_update_cancel"
android:text="取消"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="40dp"
android:layout_gravity="center"
android:textSize="12dp"/>
</LinearLayout>
</LinearLayout>
12 DialogListener.java
public interface DialogListener {
void sendMessage();
}
1
2
3
13.MainActivity.java
public class MainActivity extends AppCompatActivity{
private Button mButtonAdd;
private RecyclerView mRecyclerView;
private UserAdapter mAdapter;
private ArrayList<User> mUsers;
private ImageView mImageView;
private EditText mEditText;
private DialogListener mListener = new DialogListener() {
@Override
public void sendMessage() {
mAdapter.updateView();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
init();
initData();
mRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
mAdapter = new UserAdapter(MainActivity.this,mUsers);
mRecyclerView.setAdapter(mAdapter);
mButtonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AddDialog(MainActivity.this,mListener).show();
}
});
//点击查找
mImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = mEditText.getText().toString();
if (name.equals("")){
Toast.makeText(MainActivity.this,"查询名字不允许为空",Toast.LENGTH_SHORT).show();
}else {
User user = UserDao.getUserDao(MainActivity.this).getUserByName(name);
UpdateDialog dialog = new UpdateDialog(MainActivity.this,user,mListener);
dialog.show();
}
}
});
}
private void init(){
mButtonAdd = findViewById(R.id.main_button_add);
mRecyclerView = findViewById(R.id.main_recycler_view);
mEditText = findViewById(R.id.main_edit_name);
mImageView = findViewById(R.id.main_image_find);
}
void initData(){
mUsers = UserDao.getUserDao(MainActivity.this).getAllUser();
}
}
14.主布局文件activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp">
<EditText
android:id="@+id/main_edit_name"
android:layout_gravity="bottom"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:hint="请输入需要查询的名字"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"/>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageView
android:id="@+id/main_image_find"
android:src="@drawable/find"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_centerInParent="true"/>
</RelativeLayout>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/main_recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:id="@+id/main_button_add"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="添加联系人"/>
</LinearLayout>
15.子布局文件 info_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="100dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<RelativeLayout
android:background="#F8F4D3"
android:layout_width="80dp"
android:layout_height="match_parent">
<ImageView
android:id="@+id/info_image"
android:layout_centerInParent="true"
android:src="@drawable/girl"
android:layout_width="60dp"
android:layout_height="60dp"/>
</RelativeLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:background="#E1F5EC"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2">
<TextView
android:id="@+id/info_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:text="大青儿"
android:textSize="25dp"
android:textColor="#333"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="@+id/info_phone"
android:layout_gravity="center"
android:text="电话:13222240503"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/info_address"
android:layout_gravity="center"
android:text="地址:江苏苏州"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#666"/>
</LinearLayout>
看完了这篇文章,相信你对“如何使用Sqlite+RecyclerView+Dialog对数据进行增删改查”有了一定的了解,如果想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!
内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/151384.html