简易电话薄的项目

这两天帮朋友做了一个小项目,又算是复习了一下学过的东西

简易通讯录功能概述


图片

信息存储在数据库SQL 数据库中,使用JDBC驱动程序连接操作。

用户信息表结构如下
图片

电话信息表结构如下

图片



图片

图片

图片

图片

图片

图片

图片

图片

图片

图片

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Android电话簿应用程序,可以实现添加、删除、查找和编辑联系人的功能。 布局文件 `activity_main.xml`: ```xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp"> <EditText android:id="@+id/edit_name" android:hint="姓名" android:layout_width="match_parent" android:layout_height="wrap_content"/> <EditText android:id="@+id/edit_phone" android:hint="电话" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="phone"/> <Button android:id="@+id/btn_add" android:text="添加" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onAddClick"/> <ListView android:id="@+id/list_contacts" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> </LinearLayout> ``` Java 代码文件 `MainActivity.java`: ```java import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.ListView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private EditText mEditName; private EditText mEditPhone; private ListView mListContacts; private ArrayAdapter<String> mAdapter; private ArrayList<String> mContacts; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mEditName = findViewById(R.id.edit_name); mEditPhone = findViewById(R.id.edit_phone); mListContacts = findViewById(R.id.list_contacts); mContacts = new ArrayList<>(); mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mContacts); mListContacts.setAdapter(mAdapter); mListContacts.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { String contact = mContacts.get(position); String name = contact.split(":")[0]; String phone = contact.split(":")[1]; mEditName.setText(name); mEditPhone.setText(phone); } }); } public void onAddClick(View view) { String name = mEditName.getText().toString().trim(); String phone = mEditPhone.getText().toString().trim(); if (name.isEmpty() || phone.isEmpty()) { Toast.makeText(this, "请输入姓名和电话", Toast.LENGTH_SHORT).show(); return; } String contact = name + ":" + phone; mContacts.add(contact); mAdapter.notifyDataSetChanged(); mEditName.setText(""); mEditPhone.setText(""); } public void onDeleteClick(View view) { int position = mListContacts.getCheckedItemPosition(); if (position == ListView.INVALID_POSITION) { Toast.makeText(this, "请选择一项联系人", Toast.LENGTH_SHORT).show(); return; } mContacts.remove(position); mAdapter.notifyDataSetChanged(); } public void onUpdateClick(View view) { int position = mListContacts.getCheckedItemPosition(); if (position == ListView.INVALID_POSITION) { Toast.makeText(this, "请选择一项联系人", Toast.LENGTH_SHORT).show(); return; } String name = mEditName.getText().toString().trim(); String phone = mEditPhone.getText().toString().trim(); if (name.isEmpty() || phone.isEmpty()) { Toast.makeText(this, "请输入姓名和电话", Toast.LENGTH_SHORT).show(); return; } String contact = name + ":" + phone; mContacts.set(position, contact); mAdapter.notifyDataSetChanged(); mEditName.setText(""); mEditPhone.setText(""); } public void onSearchClick(View view) { String keyword = mEditName.getText().toString().trim(); if (keyword.isEmpty()) { Toast.makeText(this, "请输入关键字", Toast.LENGTH_SHORT).show(); return; } int position = -1; for (int i = 0; i < mContacts.size(); i++) { String contact = mContacts.get(i); String name = contact.split(":")[0]; if (name.contains(keyword)) { position = i; break; } } if (position != -1) { mListContacts.setItemChecked(position, true); } else { Toast.makeText(this, "未找到符合条件的联系人", Toast.LENGTH_SHORT).show(); } } } ``` 这个应用程序提供了四个按钮: - “添加”:将输入的姓名和电话添加到联系人列表中。 - “删除”:删除当前选定的联系人。 - “编辑”:将输入的姓名和电话更新到当前选定的联系人。 - “搜索”:根据输入的关键字查找联系人。 在代码中,我们使用 ArrayList 来存储联系人,并使用 ArrayAdapter 将其绑定到 ListView 上。在添加、删除、编辑和搜索联系人时,我们只需要对 ArrayList 进行操作,然后通过调用 ArrayAdapter 的 notifyDataSetChanged 方法来更新 ListView。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值