通讯录首页:

添加联系人页面:

修改联系人:

删除联系人:

程序代码:

MainActivity.java

package com.example.myapplication;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import androidx.appcompat.app.AlertDialog;import android.content.DialogInterface;import android.view.View;import android.widget.AdapterView;import android.widget.EditText;import android.widget.ImageView;import android.widget.ListView;import android.widget.Toast;import java.util.List;public class MainActivity extends AppCompatActivity{private ImageView iv_add;private ListView lv_show;private EditText et_name,et_phone;private MyAdapter myAdapter;private DBHelper dbHelper;private List userList;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);iv_add = findViewById(R.id.iv_add);lv_show = findViewById(R.id.lv_show);if (userList != null) {userList.clear();}dbHelper = new DBHelper(MainActivity.this, "user.db", null, 1);updateListView();iv_add.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {addData();}});lv_show.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView

MyAdapter.java

package com.example.myapplication;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;import java.util.List;public class MyAdapter extends BaseAdapter {//使用list,list会存储数据库中note表所有记录。private List list;//用于将某个布局转换为view的对象。private LayoutInflater layoutInflater;//当创建MyAdapter对象的时候,需要list的数据public MyAdapter(List list, Context context){this.list=list;layoutInflater=LayoutInflater.from(context);}@Overridepublic int getCount() {return list.size();}@Overridepublic Object getItem(int position) {return list.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder viewHolder;if (convertView==null){convertView=layoutInflater.inflate(R.layout.item,null,false);viewHolder=new ViewHolder(convertView);convertView.setTag(viewHolder);}else {viewHolder=(ViewHolder)convertView.getTag();}//将数据库中的内容加载到对应的控件上User user =(User) getItem(position);viewHolder.tv_showName.setText(user.getName());viewHolder.tv_showPhone.setText(user.getPhone());return convertView;}//用于给item的视图加载数据内容。class ViewHolder{TextView tv_showName,tv_showPhone;public ViewHolder(View view){tv_showName=view.findViewById(R.id.tv_showName);tv_showPhone=view.findViewById(R.id.tv_showPhone);}}}

DBHelper.java

package com.example.myapplication;import android.content.ContentValues;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;import androidx.annotation.Nullable;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;public class DBHelper extends SQLiteOpenHelper {private SQLiteDatabase db;ContentValues contentValues = new ContentValues();public DBHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {super(context, name, factory, version);db=this.getWritableDatabase();}@Overridepublic void onCreate(SQLiteDatabase db) {db.execSQL("create table user(id integer primary key autoincrement,name text,phone text)");}//添加数据public boolean insert(String name,String phone) {contentValues.put("name", name);contentValues.put("phone", phone);long result = db.insert("user", null, contentValues);return result > 0 ? true : false;}public boolean delete(String id){int result=db.delete("user","id=?",new String[]{id});return result>0?true:false;}//修改数据,根据id进行修改public boolean update(String id,String name,String phone){contentValues.put("id",id);contentValues.put("name",name);contentValues.put("phone",phone);int result=db.update("user",contentValues,"id=?",new String[]{id});return result>0?true:false;}//查询数据,查询表中的所有内容,将查询的内容用note的对象属性进行存储,并将该对象存入集合中。public List query(){List list=new ArrayList();Cursor result=db.query("user",null,null,null,null,null,null,null);if (result!=null){while (result.moveToNext()) {User user = new User();user.setId(String.valueOf(result.getInt(0)));user.setName(result.getString(1)); //1即第二列,指内容user.setPhone(result.getString(2));//2即第三列,指时间list.add(user);}result.close();}return list;}public User get(String name) {User user=new User();Cursor result=db.query("user",null,"name=?",new String[]{name},null,null,null,null);if (result.getCount()==1){result.moveToFirst();user.setName(result.getString(1));user.setPhone(result.getString(2));return user;}else {user.setName(null);user.setPhone(null);return user;}}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}}

User.java

package com.example.myapplication;public class User {private String id;private String name;private String phone;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}public String getName() {return name;}public void setName(String name) {this.name = name;}}

activity_main.xml

dialog.xml

item.xml

colors.xml

#FFBB86FC#FF6200EE#FF3700B3#FF03DAC5#FF018786#FF000000#FFFFFFFF#0B132B#A49DBE#D5CFE1#E9E7EF#D8C4C4

详细见:https://gitee.com/love1213/Android-Studio-Contacts.git