Android Studio实现通讯录项目完整代码
时间: 2023-11-02 08:05:27 浏览: 113
很抱歉,由于通讯录项目的代码比较长,无法在此一一列出。但是,我可以提供一些实现通讯录项目的基本步骤和关键代码片段:
1. 创建联系人实体类(Contact)
```java
public class Contact {
private String name;
private String phoneNumber;
private String email;
public Contact(String name, String phoneNumber, String email) {
this.name = name;
this.phoneNumber = phoneNumber;
this.email = email;
}
// getters and setters
}
```
2. 创建数据库帮助类(DatabaseHelper)
```java
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "contacts.db";
private static final int DATABASE_VERSION = 1;
private static final String CREATE_CONTACTS_TABLE = "CREATE TABLE " + ContactContract.ContactEntry.TABLE_NAME + " ("
+ ContactContract.ContactEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ ContactContract.ContactEntry.COLUMN_NAME + " TEXT NOT NULL, "
+ ContactContract.ContactEntry.COLUMN_PHONE_NUMBER + " TEXT NOT NULL, "
+ ContactContract.ContactEntry.COLUMN_EMAIL + " TEXT);";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(CREATE_CONTACTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + ContactContract.ContactEntry.TABLE_NAME);
onCreate(sqLiteDatabase);
}
}
```
3. 创建联系人数据表契约类(ContactContract)
```java
public class ContactContract {
public static final class ContactEntry implements BaseColumns {
public static final String TABLE_NAME = "contacts";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_PHONE_NUMBER = "phone_number";
public static final String COLUMN_EMAIL = "email";
}
}
```
4. 创建添加联系人界面(AddContactActivity)
```java
public class AddContactActivity extends AppCompatActivity {
private EditText etName;
private EditText etPhoneNumber;
private EditText etEmail;
private DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_contact);
etName = findViewById(R.id.et_name);
etPhoneNumber = findViewById(R.id.et_phone_number);
etEmail = findViewById(R.id.et_email);
databaseHelper = new DatabaseHelper(this);
}
public void onSaveButtonClick(View view) {
String name = etName.getText().toString();
String phoneNumber = etPhoneNumber.getText().toString();
String email = etEmail.getText().toString();
if (TextUtils.isEmpty(name) || TextUtils.isEmpty(phoneNumber)) {
Toast.makeText(this, "Name and phone number are required", Toast.LENGTH_SHORT).show();
return;
}
Contact contact = new Contact(name, phoneNumber, email);
long result = databaseHelper.insertContact(contact);
if (result == -1) {
Toast.makeText(this, "Failed to save contact", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Contact saved", Toast.LENGTH_SHORT).show();
finish();
}
}
}
```
5. 创建显示联系人列表界面(ContactListActivity)
```java
public class ContactListActivity extends AppCompatActivity {
private ListView listView;
private DatabaseHelper databaseHelper;
private List<Contact> contactList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_list);
listView = findViewById(R.id.list_view);
databaseHelper = new DatabaseHelper(this);
contactList = databaseHelper.getAllContacts();
ContactListAdapter adapter = new ContactListAdapter(this, contactList);
listView.setAdapter(adapter);
}
}
```
6. 创建联系人列表适配器(ContactListAdapter)
```java
public class ContactListAdapter extends ArrayAdapter<Contact> {
private Context context;
private List<Contact> contactList;
public ContactListAdapter(Context context, List<Contact> contactList) {
super(context, R.layout.item_contact, contactList);
this.context = context;
this.contactList = contactList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.item_contact, null);
}
Contact contact = contactList.get(position);
TextView tvName = view.findViewById(R.id.tv_name);
TextView tvPhoneNumber = view.findViewById(R.id.tv_phone_number);
tvName.setText(contact.getName());
tvPhoneNumber.setText(contact.getPhoneNumber());
return view;
}
}
```
7. 在数据库帮助类中添加插入联系人和获取所有联系人的方法
```java
public long insertContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(ContactContract.ContactEntry.COLUMN_NAME, contact.getName());
values.put(ContactContract.ContactEntry.COLUMN_PHONE_NUMBER, contact.getPhoneNumber());
values.put(ContactContract.ContactEntry.COLUMN_EMAIL, contact.getEmail());
long result = db.insert(ContactContract.ContactEntry.TABLE_NAME, null, values);
db.close();
return result;
}
public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<>();
String selectQuery = "SELECT * FROM " + ContactContract.ContactEntry.TABLE_NAME;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
String name = cursor.getString(cursor.getColumnIndex(ContactContract.ContactEntry.COLUMN_NAME));
String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactContract.ContactEntry.COLUMN_PHONE_NUMBER));
String email = cursor.getString(cursor.getColumnIndex(ContactContract.ContactEntry.COLUMN_EMAIL));
Contact contact = new Contact(name, phoneNumber, email);
contactList.add(contact);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return contactList;
}
```
以上就是Android Studio实现通讯录项目的基本步骤和关键代码片段。
阅读全文