用android编写一个通讯录的代码
时间: 2023-09-10 08:15:31 浏览: 140
以下是一个简单的通讯录应用程序的代码,使用 Android Studio 中的 Java 编写。该应用程序允许用户添加、编辑和删除联系人,并将联系人信息保存在 SQLite 数据库中。
1. 首先,在 Android Studio 中创建一个新项目,并选择空活动。
2. 创建一个新布局文件 contact_item.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="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/nameTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp" />
<TextView
android:id="@+id/phoneTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp" />
<TextView
android:id="@+id/emailTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp" />
</LinearLayout>
```
3. 创建一个新布局文件 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">
<ListView
android:id="@+id/contactListView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:id="@+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Contact"
android:layout_gravity="center_horizontal" />
</LinearLayout>
```
4. 创建一个新的 Java 类 Contact,用于表示联系人对象。
```
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;
}
}
```
5. 创建一个新的 Java 类 ContactAdapter,用于将联系人对象绑定到 ListView 中的布局。
```
public class ContactAdapter extends ArrayAdapter<Contact> {
private List<Contact> contactList;
private Context context;
public ContactAdapter(List<Contact> contactList, Context context) {
super(context, R.layout.contact_item, contactList);
this.contactList = contactList;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.contact_item, parent, false);
TextView nameTextView = view.findViewById(R.id.nameTextView);
TextView phoneTextView = view.findViewById(R.id.phoneTextView);
TextView emailTextView = view.findViewById(R.id.emailTextView);
Contact contact = contactList.get(position);
nameTextView.setText(contact.getName());
phoneTextView.setText(contact.getPhone());
emailTextView.setText(contact.getEmail());
return view;
}
}
```
6. 在 MainActivity 类中添加以下代码,以初始化 ListView 和添加按钮,并在添加按钮被单击时打开一个新的 Activity 来添加联系人。
```
public class MainActivity extends AppCompatActivity {
private ListView contactListView;
private Button addButton;
private List<Contact> contactList;
private ContactAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactListView = findViewById(R.id.contactListView);
addButton = findViewById(R.id.addButton);
contactList = new ArrayList<>();
adapter = new ContactAdapter(contactList, this);
contactListView.setAdapter(adapter);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, AddContactActivity.class);
startActivityForResult(intent, 1);
}
});
}
}
```
7. 创建一个新的 Java 类 AddContactActivity,用于显示添加联系人表单,并在提交表单时将联系人信息插入到 SQLite 数据库中。
```
public class AddContactActivity extends AppCompatActivity {
private EditText nameEditText;
private EditText phoneEditText;
private EditText emailEditText;
private Button saveButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_contact);
nameEditText = findViewById(R.id.nameEditText);
phoneEditText = findViewById(R.id.phoneEditText);
emailEditText = findViewById(R.id.emailEditText);
saveButton = findViewById(R.id.saveButton);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = nameEditText.getText().toString();
String phone = phoneEditText.getText().toString();
String email = emailEditText.getText().toString();
if (!name.isEmpty() && !phone.isEmpty() && !email.isEmpty()) {
Contact contact = new Contact(0, name, phone, email);
addContact(contact);
setResult(RESULT_OK);
finish();
} else {
Toast.makeText(AddContactActivity.this, "Please fill in all fields", Toast.LENGTH_SHORT).show();
}
}
});
}
private void addContact(Contact contact) {
ContactDatabase db = new ContactDatabase(this);
db.addContact(contact);
db.close();
}
}
```
8. 创建一个新的 Java 类 ContactDatabase,用于处理 SQLite 数据库操作。
```
public class ContactDatabase extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "contact_database";
private static final String TABLE_NAME = "contacts";
private static final String ID_COLUMN = "id";
private static final String NAME_COLUMN = "name";
private static final String PHONE_COLUMN = "phone";
private static final String EMAIL_COLUMN = "email";
private Context context;
public ContactDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_NAME + "("
+ ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ NAME_COLUMN + " TEXT,"
+ PHONE_COLUMN + " TEXT,"
+ EMAIL_COLUMN + " TEXT" + ")";
sqLiteDatabase.execSQL(CREATE_CONTACTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(sqLiteDatabase);
}
public void addContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(NAME_COLUMN, contact.getName());
values.put(PHONE_COLUMN, contact.getPhone());
values.put(EMAIL_COLUMN, contact.getEmail());
db.insert(TABLE_NAME, null, values);
db.close();
}
public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<>();
String selectQuery = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact(
cursor.getInt(0),
cursor.getString(1),
cursor.getString(2),
cursor.getString(3)
);
contactList.add(contact);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return contactList;
}
public void deleteContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NAME, ID_COLUMN + " = ?", new String[]{String.valueOf(contact.getId())});
db.close();
}
public void updateContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(NAME_COLUMN, contact.getName());
values.put(PHONE_COLUMN, contact.getPhone());
values.put(EMAIL_COLUMN, contact.getEmail());
db.update(TABLE_NAME, values, ID_COLUMN + " = ?", new String[]{String.valueOf(contact.getId())});
db.close();
}
}
```
这是一个简单的通讯录应用程序的代码,它允许用户添加、编辑和删除联系人,并将联系人信息保存在 SQLite 数据库中。
阅读全文