Android手机开发——通讯录
实现增加、查询、修改、删除的功能,输入联系人信息,点击“添加”按钮,可以添加联系人信息到数据库;点击“查询”按钮,会发现添加的联系人信息显示在界面中;重新输入联系人电话,点击“修改”按钮,可以修改该联系人的电话,再进行查询发现联系人电话已经修改;点击“删除”按钮,会将数据库中该联系人的所有记录删除。
主界面的布局文件代码
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="100dp">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:" />
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入姓名"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="电话:" />
android:id="@+id/et_tel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入手机号码"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加"
android:background="#b9b9ff"/>
android:id="@+id/btn_query"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查询"
android:background="#dcb5ff"/>
android:id="@+id/btn_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改"
android:background="#e6caff"/>
android:id="@+id/btn_del"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除"
android:background="#acd6ff"/>
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" />
MyHelper.java
public class MyHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION=2;
private static final String DATABASE_NAME="mydb";
private static final String sql="CREATE TABLE directory("+
"_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),"
+"phone VARCHAR(20))";
public MyHelper(Context context){
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
MyDBManager.java
public class MyDBManager {
private MyHelper myHelper;
private SQLiteDatabase sqLiteDatabase;
public MyDBManager(Context context){
myHelper=new MyHelper(context);
sqLiteDatabase=myHelper.getReadableDatabase();
}
public void getConnect(){
sqLiteDatabase=myHelper.getWritableDatabase();
}
public void releaseConnect(){
if(sqLiteDatabase!=null){
sqLiteDatabase.close();
}
}
public void insert(Person data){
ContentValues values =new ContentValues();
values.put("name",data.getName());
values.put("phone",data.getPhone());
sqLiteDatabase.insert("directory",null,values);
}
public ArrayListfindAll(){
ArrayList list =new ArrayList<>();
Person person =null;
Cursor cursor=sqLiteDatabase.query("directory",null,null,null,null,null,null);
while(cursor.moveToNext()){
Person person1=new Person();
person1.setName(cursor.getString(1));
person1.setPhone(cursor.getString(2));
list.add(person1);
}
cursor.close();
return list;
}
public void updateBysql(String clause,String data,String[] args){
ContentValues values=new ContentValues();
values.put("phone",data);
sqLiteDatabase.update("directory",values,clause,args);
}
public void delete(String clause,String[] args){
sqLiteDatabase.delete("directory",clause,args);
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private EditText et_name,et_phone;
private Button btn_add,btn_find,btn_update,btn_del;
private TextView tv_show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_name=(EditText)findViewById(R.id.et_name);
et_phone=(EditText)findViewById(R.id.et_tel);
btn_add=(Button)findViewById(R.id.btn_add);
btn_find=(Button)findViewById(R.id.btn_query);
tv_show=(TextView)findViewById(R.id.tv_show);
btn_update=(Button)findViewById(R.id.btn_update);
btn_del=(Button)findViewById(R.id.btn_del);
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name =et_name.getText().toString().trim();
String phone=et_phone.getText().toString().trim();
if(TextUtils.isEmpty(name)&&TextUtils.isEmpty(phone)){
Toast.makeText(MainActivity.this,"姓名和电话号码不能为空",Toast.LENGTH_SHORT).show();
}else {
Person person=new Person();
person.setName(name);
person.setPhone(phone);
MyDBManager myDBManager=new MyDBManager(MainActivity.this);
myDBManager.getConnect();
myDBManager.insert(person);
myDBManager.releaseConnect();
Toast.makeText(MainActivity.this,"保存成功",Toast.LENGTH_SHORT).show();
}
}
});
btn_find.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MyDBManager myDBManager=new MyDBManager(MainActivity.this);
ArrayList list =myDBManager.findAll();
tv_show.setText("");
for (Person p:list){
tv_show.append("姓名"+p.getName()+";"+"电话"+p.getPhone()+"\n");
}
myDBManager.releaseConnect();
}
});
btn_update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name =et_name.getText().toString().trim();
String phone=et_phone.getText().toString();
MyDBManager myDBManager=new MyDBManager(MainActivity.this);
myDBManager.getConnect();
myDBManager.updateBysql("name=?",phone,new String[] {name});
myDBManager.releaseConnect();
Toast.makeText(MainActivity.this,"信息已修改",Toast.LENGTH_SHORT).show();
}
});
btn_del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name =et_name.getText().toString().trim();
String phone =et_phone.getText().toString().trim();
MyDBManager myDBManager= new MyDBManager(MainActivity.this);
myDBManager.getConnect();
myDBManager.delete("name=?",new String[]{name});
myDBManager.releaseConnect();
Toast.makeText(MainActivity.this,"信息已删除",Toast.LENGTH_SHORT).show();
}
});
}
}