一、实验要求
通过线性布局和相对布局来搭建Activity界面,在MainActivity中编写逻辑代码,运行程序,输入两条联系人信息分别点击“添加”“查询”“修改”“删除”按钮。实现联系人信息的添加、查询、修改以及删除功能。操作成功后,通过弹出的消息内容显示对应操作。下图界面仅供参考,可根据功能另外设计界面。
二、实验结果展示
1.没有数据,和添加数据
2.查询,和修改
3.修改后查询,和删除
4.删除后查询
三、代码展示
1.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:background="@drawable/bg"
android:padding="16dp"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="222dp"
android:background="@drawable/boiledmeat" />
<LinearLayout
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓 名:"
android:textSize="28sp"/>
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入姓名"
android:textSize="26sp"/>
</LinearLayout>
<LinearLayout
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="电 话:"
android:textSize="28sp"/>
<EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入手机号码"
android:textSize="26sp"
tools:ignore="VisualLintTextFieldSize" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginRight="2dp"
android:background="@color/white"
android:text="添加"
android:textSize="18sp"
/>
<Button
android:id="@+id/btn_query"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginRight="2dp"
android:background="@color/white"
android:text="查询"
android:textSize="18sp"/>
<Button
android:id="@+id/btn_update"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginRight="2dp"
android:background="@color/white"
android:text="修改"
android:textSize="18sp"/>
<Button
android:id="@+id/btn_delete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginRight="2dp"
android:background="@color/white"
android:text="删除"
android:textSize="18sp"/>
</LinearLayout>
<TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25sp"
android:textSize="30sp"
android:background="@color/white"/>
</LinearLayout>
2.java部分
package com.example.a5;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.content.Context;
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 {
MyHelper myHelper;
private EditText mEtName, mEtPhone;
private TextView mTvShow;
private Button mBtnAdd,mBtnQuery,mBtnDelete,mBtnUpdata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myHelper = new MyHelper(this);
init();
}
public void init() {
mEtName = findViewById(R.id.et_name);
mEtPhone = findViewById(R.id.et_phone);
mTvShow = findViewById(R.id.tv_show);
mBtnAdd = findViewById(R.id.btn_add);
mBtnDelete = findViewById(R.id.btn_delete);
mBtnQuery = findViewById(R.id.btn_query);
mBtnUpdata = findViewById(R.id.btn_update);
mBtnAdd.setOnClickListener(this);
mBtnUpdata.setOnClickListener(this);
mBtnQuery.setOnClickListener(this);
mBtnDelete.setOnClickListener(this);
}
@Override
public void onClick(View view) {
String name, phone;
SQLiteDatabase db;
ContentValues values;
switch (view.getId()) {
case R.id.btn_add:
name = mEtName.getText().toString();
phone = mEtPhone.getText().toString();
db = myHelper.getWritableDatabase();
//创建ContentValues对象
values = new ContentValues();
values.put("name", name);
values.put("phone", phone);
db.insert("information", null, values);
Toast.makeText(this, "信息已添加", Toast.LENGTH_SHORT).show();
db.close();
break;
case R.id.btn_query:
db = myHelper.getReadableDatabase();
//行数集合
Cursor cursor = db.query("information", null, null, null, null, null, null);
if (cursor.getCount() == 0) {
mTvShow.setText("");
Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
}
else {
cursor.moveToFirst();
mTvShow.setText("Name : " + cursor.getString(0) + " ; Tel : " + cursor.getString(1));
}
while (cursor.moveToNext()) {
mTvShow.append("\n" + "Name : " + cursor.getString(0) + " ; Tel : " + cursor.getString(1));
}
cursor.close();
db.close();
break;
case R.id.btn_update:
db = myHelper.getWritableDatabase();
values = new ContentValues();
//只能修改电话,不能修改姓名
values.put("phone", phone = mEtPhone.getText().toString());
db.update("information", values, "name=?", new String[]{mEtName.getText().toString()});
Toast.makeText(this, "信息已修改", Toast.LENGTH_SHORT).show();
db.close();
break;
case R.id.btn_delete:
db = myHelper.getWritableDatabase();
//这种删除是 删除所有数据
db.delete("information", null, null);
mTvShow.setText("");
Toast.makeText(this, "信息已删除", Toast.LENGTH_SHORT).show();
db.close();
break;
}
}
class MyHelper extends SQLiteOpenHelper {
public MyHelper( Context context) {
super(context,"111.db",null,1);
}
@Override
public void onCreate(SQLiteDatabase db) {
//创建information表
db.execSQL("CREATE TABLE information(name VARCHAR(20) PRIMARY KEY,phone VARCHAR(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
}
}
}
四、实验总结
数据查询时要用到cursor,注意这个下标是从0开始