android+studio通讯录
时间: 2023-12-15 16:33:08 浏览: 147
以下是使用Android Studio实现通讯录的一般步骤:
1.创建一个新项目并添加必要的依赖项,例如SQLite数据库。
2.创建一个布局文件来显示通讯录列表。可以使用ListView来显示通讯录列表。
3.创建一个Java类来处理通讯录数据。这个类应该包含添加、删除、更新和查询通讯录数据的方法。
4.在应用程序中使用适配器将通讯录数据绑定到ListView上。
5.添加一个搜索框,以便用户可以搜索通讯录中的联系人。
6.添加一个详细视图,以便用户可以查看和编辑联系人的详细信息。
7.测试应用程序并进行必要的调试。
以下是一个简单的示例代码,用于在Android Studio中实现通讯录:
1.布局文件:
```xml
<ListView
android:id="@+id/friend"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
2.Java类:
```java
public class Contact {
private int id;
private String name;
private String phone;
public Contact(int id, String name, String phone) {
this.id = id;
this.name = name;
this.phone = phone;
}
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 class ContactDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "contact.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "contact"; 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 CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_NAME + " TEXT, " +
COLUMN_PHONE + " TEXT)";
public ContactDatabaseHelper(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);
}
public void addContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, contact.getName());
values.put(COLUMN_PHONE, contact.getPhone());
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(cursor.getColumnIndex(COLUMN_ID)),
cursor.getString(cursor.getColumnIndex(COLUMN_NAME)),
cursor.getString(cursor.getColumnIndex(COLUMN_PHONE))
);
contactList.add(contact);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return contactList;
}
public void updateContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, contact.getName());
values.put(COLUMN_PHONE, contact.getPhone());
db.update(TABLE_NAME, values, COLUMN_ID + " = ?",
new String[]{String.valueOf(contact.getId())});
db.close();
}
public void deleteContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NAME, COLUMN_ID + " = ?",
new String[]{String.valueOf(contact.getId())});
db.close();
}
}
public class ContactAdapter extends ArrayAdapter<Contact> {
private Context context;
private List<Contact> contactList;
public ContactAdapter(Context context, List<Contact> contactList) {
super(context, R.layout.list_item_contact, contactList);
this.context = context;
this.contactList = contactList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_item_contact, parent, false);
TextView nameTextView = rowView.findViewById(R.id.nameTextView);
TextView phoneTextView = rowView.findViewById(R.id.phoneTextView);
Contact contact = contactList.get(position);
nameTextView.setText(contact.getName());
phoneTextView.setText(contact.getPhone());
return rowView;
}
}
public class MainActivity extends AppCompatActivity {
private ListView friendListView;
private ContactDatabaseHelper dbHelper;
private List<Contact> contactList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
friendListView = findViewById(R.id.friend);
dbHelper = new ContactDatabaseHelper(this);
contactList = dbHelper.getAllContacts();
ContactAdapter adapter = new ContactAdapter(this, contactList);
friendListView.setAdapter(adapter);
}
}
```
阅读全文