用安卓写一个课表,可以添加课表、查看课表、修改课表,数据库用Sqlite,编程语言用java,带有注释
时间: 2023-11-28 19:50:35 浏览: 83
好的,下面是一个简单的安卓课表应用,包含添加课表、查看课表和修改课表功能,并使用 SQLite 数据库进行数据存储。注释已经添加在代码中,方便理解。
```java
// MainActivity.java
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText mCourseNameEditText; // 课程名称编辑框
private EditText mTeacherNameEditText; // 教师名称编辑框
private EditText mClassroomEditText; // 教室编辑框
private EditText mWeekdayEditText; // 星期编辑框
private EditText mTimeEditText; // 时间编辑框
private TextView mScheduleTextView; // 课表文本显示框
private SQLiteDatabase mDatabase; // 数据库对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化 UI 组件
mCourseNameEditText = findViewById(R.id.course_name_edit_text);
mTeacherNameEditText = findViewById(R.id.teacher_name_edit_text);
mClassroomEditText = findViewById(R.id.classroom_edit_text);
mWeekdayEditText = findViewById(R.id.weekday_edit_text);
mTimeEditText = findViewById(R.id.time_edit_text);
mScheduleTextView = findViewById(R.id.schedule_text_view);
Button addButton = findViewById(R.id.add_button);
Button viewButton = findViewById(R.id.view_button);
Button updateButton = findViewById(R.id.update_button);
// 创建或打开数据库
mDatabase = openOrCreateDatabase("schedule.db", MODE_PRIVATE, null);
// 创建课表表格
String createTableSql = "CREATE TABLE IF NOT EXISTS schedule (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT," +
"course_name TEXT," +
"teacher_name TEXT," +
"classroom TEXT," +
"weekday INTEGER," +
"time TEXT)";
mDatabase.execSQL(createTableSql);
// 添加按钮点击事件监听器
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String courseName = mCourseNameEditText.getText().toString();
String teacherName = mTeacherNameEditText.getText().toString();
String classroom = mClassroomEditText.getText().toString();
String weekdayString = mWeekdayEditText.getText().toString();
String time = mTimeEditText.getText().toString();
// 检查输入是否为空
if (TextUtils.isEmpty(courseName) || TextUtils.isEmpty(weekdayString) || TextUtils.isEmpty(time)) {
Toast.makeText(MainActivity.this, "请填写完整信息", Toast.LENGTH_SHORT).show();
return;
}
// 解析星期
int weekday = Integer.parseInt(weekdayString);
// 插入数据
ContentValues values = new ContentValues();
values.put("course_name", courseName);
values.put("teacher_name", teacherName);
values.put("classroom", classroom);
values.put("weekday", weekday);
values.put("time", time);
long rowId = mDatabase.insert("schedule", null, values);
// 显示结果
if (rowId != -1) {
Toast.makeText(MainActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "添加失败", Toast.LENGTH_SHORT).show();
}
// 清空输入框
mCourseNameEditText.setText("");
mTeacherNameEditText.setText("");
mClassroomEditText.setText("");
mWeekdayEditText.setText("");
mTimeEditText.setText("");
}
});
// 查看按钮点击事件监听器
viewButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 查询数据
Cursor cursor = mDatabase.query("schedule", null, null, null, null, null, null);
StringBuilder resultBuilder = new StringBuilder();
if (cursor.moveToFirst()) {
do {
int id = cursor.getInt(cursor.getColumnIndex("_id"));
String courseName = cursor.getString(cursor.getColumnIndex("course_name"));
String teacherName = cursor.getString(cursor.getColumnIndex("teacher_name"));
String classroom = cursor.getString(cursor.getColumnIndex("classroom"));
int weekday = cursor.getInt(cursor.getColumnIndex("weekday"));
String time = cursor.getString(cursor.getColumnIndex("time"));
resultBuilder.append(String.format("%d\t%s\t%s\t%s\t%d\t%s\n", id, courseName, teacherName, classroom, weekday, time));
} while (cursor.moveToNext());
}
cursor.close();
// 显示结果
mScheduleTextView.setText(resultBuilder.toString());
}
});
// 修改按钮点击事件监听器
updateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String courseIdString = mCourseNameEditText.getText().toString();
String weekdayString = mWeekdayEditText.getText().toString();
String time = mTimeEditText.getText().toString();
// 检查输入是否为空
if (TextUtils.isEmpty(courseIdString) || TextUtils.isEmpty(weekdayString) || TextUtils.isEmpty(time)) {
Toast.makeText(MainActivity.this, "请填写完整信息", Toast.LENGTH_SHORT).show();
return;
}
// 解析课程 ID 和星期
int courseId = Integer.parseInt(courseIdString);
int weekday = Integer.parseInt(weekdayString);
// 更新数据
ContentValues values = new ContentValues();
values.put("weekday", weekday);
values.put("time", time);
int rowsAffected = mDatabase.update("schedule", values, "_id=?", new String[]{String.valueOf(courseId)});
// 显示结果
if (rowsAffected > 0) {
Toast.makeText(MainActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
}
// 清空输入框
mCourseNameEditText.setText("");
mTeacherNameEditText.setText("");
mClassroomEditText.setText("");
mWeekdayEditText.setText("");
mTimeEditText.setText("");
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
// 关闭数据库
mDatabase.close();
}
}
```
```xml
<!-- activity_main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/course_name_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="课程名称" />
<EditText
android:id="@+id/teacher_name_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="教师名称" />
<EditText
android:id="@+id/classroom_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="教室" />
<EditText
android:id="@+id/weekday_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="星期" />
<EditText
android:id="@+id/time_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="时间" />
<Button
android:id="@+id/add_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="添加" />
<Button
android:id="@+id/view_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查看" />
<Button
android:id="@+id/update_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="修改" />
<TextView
android:id="@+id/schedule_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
```
阅读全文