android studio使用数据库制作安卓手机通讯录
时间: 2024-09-21 07:04:41 浏览: 113
在Android Studio中使用数据库制作手机通讯录,通常我们会选择SQLite作为本地数据库,因为它轻量级且易于管理。以下是简单的步骤:
1. **设置环境**:首先,确保已安装Android SDK并配置了Android Studio,还要添加sqlite-jdbc驱动到项目的build.gradle文件中。
```groovy
dependencies {
implementation 'org.xerial:sqlite-jdbc:3.36.0'
}
```
2. **创建数据库表**:编写SQL脚本创建一个模拟通讯录的数据表,比如包含姓名(name), 联系电话(phone)和电子邮件(email)等字段。
3. **创建DAO(Data Access Object)**:这是用于与数据库交互的接口,包括插入、查询、更新和删除数据的方法。
4. **SQLiteOpenHelper**:继承自这个类并重写`onCreate()`和`onUpgrade()`方法,前者用于初始化数据库,后者处理版本升级。
5. **CRUD操作**:在业务层(如Model或Repository),通过DAO调用数据库API来增删改查联系人信息。
6. **界面展示**:在Activity或Fragment中,使用RecyclerView或者其他适配器将从数据库获取的联系人数据显示出来。
相关问题
android studio手机通讯录
Android Studio是一款Android应用开发工具。它可以用来开发、调试、打包Android应用。要在Android Studio中访问手机通讯录,需要在项目的AndroidManifest.xml文件中添加访问通讯录的权限,并使用系统提供的ContentProvider和Cursor类访问通讯录数据库。
android studio通讯录制作代码
以下是一个简单的Android Studio通讯录应用程序的制作代码示例,其中包括SQLite数据库的使用和基本的CRUD操作:
1. 首先,创建一个新的Android Studio项目,并在应用程序中添加以下依赖项:
```
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.navigation:navigation-fragment:2.3.5'
implementation 'androidx.navigation:navigation-ui:2.3.5'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.room:room-runtime:2.3.0'
annotationProcessor 'androidx.room:room-compiler:2.3.0'
}
```
2. 在应用程序中创建一个名为"Contact"的Java类,该类表示一个联系人对象,包括以下属性:姓名,电话号码和电子邮件地址。例如:
```
public class Contact {
private int id;
private String name;
private String phone;
private String email;
public Contact(int id, String name, String phone, String email) {
this.id = id;
this.name = name;
this.phone = phone;
this.email = email;
}
public int getId() {
return id;
}
public void setId(int 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 getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
```
3. 创建一个SQLiteOpenHelper类,用于创建和管理SQLite数据库。例如:
```
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "contacts.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "contacts";
private static final String COLUMN_ID = "id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_PHONE = "phone";
private static final String COLUMN_EMAIL = "email";
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
COLUMN_NAME + " TEXT," +
COLUMN_PHONE + " TEXT," +
COLUMN_EMAIL + " TEXT" + ")";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
```
4. 创建一个ContactDAO类,用于定义与数据库交互的所有方法。例如:
```
public class ContactDAO {
private SQLiteDatabase database;
private DatabaseHelper dbHelper;
public ContactDAO(Context context) {
dbHelper = new DatabaseHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public void addContact(Contact contact) {
ContentValues values = new ContentValues();
values.put(DatabaseHelper.COLUMN_NAME, contact.getName());
values.put(DatabaseHelper.COLUMN_PHONE, contact.getPhone());
values.put(DatabaseHelper.COLUMN_EMAIL, contact.getEmail());
database.insert(DatabaseHelper.TABLE_NAME, null, values);
}
public void updateContact(Contact contact) {
ContentValues values = new ContentValues();
values.put(DatabaseHelper.COLUMN_NAME, contact.getName());
values.put(DatabaseHelper.COLUMN_PHONE, contact.getPhone());
values.put(DatabaseHelper.COLUMN_EMAIL, contact.getEmail());
database.update(DatabaseHelper.TABLE_NAME, values,
DatabaseHelper.COLUMN_ID + " = ?", new String[] { String.valueOf(contact.getId()) });
}
public void deleteContact(Contact contact) {
database.delete(DatabaseHelper.TABLE_NAME,
DatabaseHelper.COLUMN_ID + " = ?", new String[] { String.valueOf(contact.getId()) });
}
public List<Contact> getAllContacts() {
List<Contact> contacts = new ArrayList<>();
Cursor cursor = database.query(DatabaseHelper.TABLE_NAME,
new String[] { DatabaseHelper.COLUMN_ID, DatabaseHelper.COLUMN_NAME,
DatabaseHelper.COLUMN_PHONE, DatabaseHelper.COLUMN_EMAIL },
null, null, null, null, null);
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact(
cursor.getInt(cursor.getColumnIndex(DatabaseHelper.COLUMN_ID)),
cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_NAME)),
cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_PHONE)),
cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_EMAIL))
);
contacts.add(contact);
} while (cursor.moveToNext());
}
cursor.close();
return contacts;
}
public Contact getContactById(int id) {
Cursor cursor = database.query(DatabaseHelper.TABLE_NAME,
new String[] { DatabaseHelper.COLUMN_ID, DatabaseHelper.COLUMN_NAME,
DatabaseHelper.COLUMN_PHONE, DatabaseHelper.COLUMN_EMAIL },
DatabaseHelper.COLUMN_ID + " = ?", new String[] { String.valueOf(id) },
null, null, null);
Contact contact = null;
if (cursor.moveToFirst()) {
contact = new Contact(
cursor.getInt(cursor.getColumnIndex(DatabaseHelper.COLUMN_ID)),
cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_NAME)),
cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_PHONE)),
cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_EMAIL))
);
}
cursor.close();
return contact;
}
}
```
5. 创建MainActivity类,用于显示所有联系人的列表视图。例如:
```
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ContactAdapter adapter;
private List<Contact> contacts = new ArrayList<>();
private ContactDAO contactDAO;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new ContactAdapter(this, contacts);
recyclerView.setAdapter(adapter);
contactDAO = new ContactDAO(this);
contactDAO.open();
contacts.addAll(contactDAO.getAllContacts());
adapter.notifyDataSetChanged();
}
@Override
protected void onDestroy() {
super.onDestroy();
contactDAO.close();
}
public void addContact(View view) {
Intent intent = new Intent(this, AddContactActivity.class);
startActivity(intent);
}
public void editContact(View view) {
int position = recyclerView.getChildAdapterPosition(view);
Contact contact = contacts.get(position);
Intent intent = new Intent(this, EditContactActivity.class);
intent.putExtra("id", contact.getId());
startActivity(intent);
}
public void deleteContact(View view) {
int position = recyclerView.getChildAdapterPosition(view);
Contact contact = contacts.get(position);
contactDAO.deleteContact(contact);
contacts.remove(contact);
adapter.notifyItemRemoved(position);
}
}
```
6. 创建一个ContactAdapter类,用于将联系人列表显示为RecyclerView。例如:
```
public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ViewHolder> {
private List<Contact> contacts;
private LayoutInflater inflater;
public ContactAdapter(Context context, List<Contact> contacts) {
this.contacts = contacts;
this.inflater = LayoutInflater.from(context);
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.contact_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Contact contact = contacts.get(position);
holder.nameTextView.setText(contact.getName());
holder.phoneTextView.setText(contact.getPhone());
holder.emailTextView.setText(contact.getEmail());
}
@Override
public int getItemCount() {
return contacts.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView nameTextView;
public TextView phoneTextView;
public TextView emailTextView;
public ViewHolder(View itemView) {
super(itemView);
nameTextView = itemView.findViewById(R.id.name_text_view);
phoneTextView = itemView.findViewById(R.id.phone_text_view);
emailTextView = itemView.findViewById(R.id.email_text_view);
}
}
}
```
7. 创建AddContactActivity类,用于处理添加新联系人的表单。例如:
```
public class AddContactActivity extends AppCompatActivity {
private ContactDAO contactDAO;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_contact);
contactDAO = new ContactDAO(this);
contactDAO.open();
}
@Override
protected void onDestroy() {
super.onDestroy();
contactDAO.close();
}
public void saveContact(View view) {
EditText nameEditText = findViewById(R.id.name_edit_text);
EditText phoneEditText = findViewById(R.id.phone_edit_text);
EditText emailEditText = findViewById(R.id.email_edit_text);
String name = nameEditText.getText().toString();
String phone = phoneEditText.getText().toString();
String email = emailEditText.getText().toString();
Contact contact = new Contact(0, name, phone, email);
contactDAO.addContact(contact);
finish();
}
}
```
8. 最后,创建布局文件和其他必要的资源文件。
以上是一个简单的Android Studio通讯录应用程序的制作代码示例。希望这些信息可以帮助您开始开发您自己的通讯录应用程序。
阅读全文