代码android studio 通讯录
时间: 2023-11-30 08:43:19 浏览: 116
以下是一个简单的Android Studio通讯录代码示例,包括添加联系人、修改联系人、删除联系人和查询联系人等功能:
```java
// 添加联系人
public void addContact(String name, String phone, String email) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, name);
values.put(KEY_PHONE, phone);
values.put(KEY_EMAIL, email);
db.insert(TABLE_CONTACTS, null, values);
db.close();
}
// 修改联系人
public int updateContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName());
values.put(KEY_PHONE, contact.getPhoneNumber());
values.put(KEY_EMAIL, contact.getEmail());
return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
}
// 删除联系人
public void deleteContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
db.close();
}
// 查询联系人
public Contact getContact(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
KEY_NAME, KEY_PHONE, KEY_EMAIL }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2), cursor.getString(3));
return contact;
}
```
阅读全文