XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/bg"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓 名:"
android:textSize="30sp"/>
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入姓名"
android:textSize="30sp"
android:layout_marginStart="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="电 话:"
android:textSize="30sp"/>
<EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入电话号码"
android:textSize="30sp"
android:layout_marginStart="10dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="15dp">
<Button
android:id="@+id/btn_add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="18dp"
android:textSize="25sp"
android:text="添加"
android:background="#B9B9FF"
android:layout_weight="1"/>
<Button
android:id="@+id/btn_query"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="18sp"
android:textSize="25sp"
android:background="#DCB5FF"
android:text="查询"
android:layout_weight="1"/>
<Button
android:id="@+id/btn_update"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="25sp"
android:text="修改"
android:background="#E6CAFF"
android:padding="18sp"
android:layout_weight="1"/>
<Button
android:id="@+id/btn_delete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="25sp"
android:background="#ACD6FF"
android:padding="18sp"
android:text="删除"
android:layout_weight="1"/>
</LinearLayout>
<TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:textSize="20sp"/>
</LinearLayout>
后端
主类:
package cn.itcast.directory;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText met_name;
private EditText met_phone;
private Button mbtn_add;
private Button mbtn_query;
private Button mbtn_update;
private Button mbtn_delete;
private TextView mtv_show;
mySqlLite mySql = new mySqlLite(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init(){
met_name = findViewById(R.id.et_name);
met_phone = findViewById(R.id.et_phone);
mbtn_add = findViewById(R.id.btn_add);
mbtn_query = findViewById(R.id.btn_query);
mbtn_update = findViewById(R.id.btn_update);
mbtn_delete = findViewById(R.id.btn_delete);
mtv_show = findViewById(R.id.tv_show);
mbtn_add.setOnClickListener(this);
mbtn_update.setOnClickListener(this);
mbtn_query.setOnClickListener(this);
mbtn_delete.setOnClickListener(this);
}
@Override
public void onClick(View view) {
String name,phone;
SQLiteDatabase db;
ContentValues values;
switch (view.getId()){
case R.id.btn_add:
name = met_name.getText().toString().trim();
phone = met_phone.getText().toString().trim();
db = mySql.getWritableDatabase();
values = new ContentValues();
values.put("name",name);
values.put("phone",phone);
db.insert("information",null,values);
Toast.makeText(this,"添加成功!",Toast.LENGTH_LONG).show();
db.close();
break;
case R.id.btn_query:
db = mySql.getReadableDatabase();
Cursor cursor = db.query("information",null,null,null,null,null,null);
if (cursor.getCount()==0){
mtv_show.setText("");
Toast.makeText(this,"无数据",Toast.LENGTH_LONG);
db.close();
}else {
cursor.moveToFirst();
mtv_show.setText("姓名:"+cursor.getString(1)+" ;电话:"+cursor.getString(2));
}
while (cursor.moveToNext()){
mtv_show.append("\n"+"姓名:"+cursor.getString(1)+" ;电话:"+cursor.getString(2));
}
cursor.close();
break;
case R.id.btn_update:
db = mySql.getWritableDatabase();
values = new ContentValues(); //要修改的数据
values.put("phone",phone = met_phone.getText().toString().trim());
db.update("information",values,"name=?",
new String[]{met_name.getText().toString()});//更新姓名
Toast.makeText(this,"信息已修改!",Toast.LENGTH_SHORT).show();
db.close();
break;
case R.id.btn_delete:
db = mySql.getWritableDatabase();
db.delete("information",null,null);
Toast.makeText(this,"已删除所有信息",Toast.LENGTH_SHORT).show();
mtv_show.setText("");
db.close();
break;
}
}
}
创建数据库工具类:
package cn.itcast.directory;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class mySqlLite extends SQLiteOpenHelper {
public mySqlLite(Context context) {
super(context,"directory.db",null,2);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("CREATE TABLE information(_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),phone VARCHAR(20))");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}