2025.3.12
今天胃疼腹泻呕吐感觉要死了
但是补了一下作业
源程序代码:
package com.example.myapplication;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText courseNameEditText, teacherEditText, locationEditText;
private Button saveButton;
private MyDatabaseHelper dbHelper;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
courseNameEditText = findViewById(R.id.courseNameEditText);
teacherEditText = findViewById(R.id.teacherEditText);
locationEditText = findViewById(R.id.locationEditText);
saveButton = findViewById(R.id.saveButton);
dbHelper = new MyDatabaseHelper(this);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String courseName = courseNameEditText.getText().toString();
String teacher = teacherEditText.getText().toString();
String location = locationEditText.getText().toString();
if (isCourseNameUnique(courseName)) {
if (isValidTeacher(teacher)) {
if (isValidLocation(location)) {
saveCourse(courseName, teacher, location);
} else {
Toast.makeText(MainActivity.this, "上课地点无效", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(MainActivity.this, "无效的任课教师", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(MainActivity.this, "课程名称重复,请重新输入", Toast.LENGTH_SHORT).show();
}
}
});
}
private boolean isCourseNameUnique(String courseName) {
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query("courses", new String[]{"name"}, "name=?", new String[]{courseName}, null, null, null);
boolean isUnique = !cursor.moveToFirst();
cursor.close();
return isUnique;
}
private boolean isValidTeacher(String teacher) {
String[] validTeachers = {"王建民", "刘立嘉", "刘丹", "杨子光", "张云霞", "武永亮", "高飞", "孙静", "黄荣峰"};
for (String validTeacher : validTeachers) {
if (validTeacher.equals(teacher)) {
return true;
}
}
return false;
}
private boolean isValidLocation(String location) {
return location.startsWith("一教") || location.startsWith("二教") || location.startsWith("三教") || location.startsWith("基教");
}
private void saveCourse(String courseName, String teacher, String location) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", courseName);
values.put("teacher", teacher);
values.put("location", location);
db.insert("courses", null, values);
db.close();
Toast.makeText(this, "课程添加成功", Toast.LENGTH_SHORT).show();
}
}
package com.example.myapplication;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "courses.db";
private static final int DATABASE_VERSION = 1;
public MyDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_COURSES_TABLE = "CREATE TABLE courses (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT," +
"name TEXT," +
"teacher TEXT," +
"location TEXT" +
")";
db.execSQL(CREATE_COURSES_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS courses");
onCreate(db);
}
}
<EditText
android:id="@+id/courseNameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入课程名称" />
<EditText
android:id="@+id/teacherEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入任课教师" />
<EditText
android:id="@+id/locationEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入上课地点" />
<Button
android:id="@+id/saveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存" />