您好,登錄后才能下訂單哦!
小編給大家分享一下如何使用Sqlite+RecyclerView+Dialog對數據進行增刪改查,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
原題目要求:
(1)通過“添加聯系人”按鈕,跳轉到“添加”信息的Dialog輸入用戶名、聯系電話、地址選擇性別,添加后顯示到聯系人列表。
(2)點擊 聯系人中的項,可以彈出Dialog,修改聯系人信息或者刪除聯系人。
(3)可以通過輸入姓名進行聯系人信息的查找,并給出信息。
說明
1.此篇博文是代碼部分,一些有關于實現的細節,感興趣的同學可以看一下此篇博文
2.建議閱讀《安卓編程權威指南3》,里面有關Sqlite的代碼的格式值得學習,或者說,整本書的代碼風格都是值得學習的
3.我會持續更新我的作業,關注不迷路哈
效果圖
代碼
目錄結構
這是我的目錄結構,adapter包下是適配器,dao包下是訪問數據庫的接口和實現方法,database包下是一些有關數據庫的類,dialog包下是自定義對話框,model包下是數據模型
添加依賴
由于使用了RecyclerView,所以得添加依賴
implementation 'com.android.support:recyclerview-v7:28.0.0'
1
具體代碼
1.User.java
public class User {
private UUID id;
private String name;
private String phone;
private String address;
private int sex;
public User() {
}
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public User(UUID id, String name, String phone, String address, int sex) {
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對數據進行增刪改查”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。