同时这也是中国大学慕课移动终端应用开发的网课作业21
原题目要求:
(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;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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";
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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) {
//不更新数据库
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
总结
如果有什么问题或改进方案,请私信联系我或者在评论区留言
码字不易,若有帮助,给个关注和赞呗
————————————————
版权声明:本文为CSDN博主「大青儿」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/baidu_41860619/java/article/details/106208417
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69975208/viewspace-2693221/ ,如需转载,请注明出处,否则将追究法律责任。