安卓写一个课表,可以添加课程和老师以及第几节,还可以查看,用Sqlite连接数据库,详细代码
时间: 2024-05-09 21:15:53 浏览: 137
我的课表,基于Android开发
好的,我可以为您提供一个简单的安卓课表应用程序,它使用SQLite数据库来存储课程和老师信息。以下是详细的代码:
1. 创建一个新的Android项目,并在build.gradle文件中添加以下依赖项:
```
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.sqlite:sqlite:2.1.0'
```
2. 在布局文件中创建一个RecyclerView来显示课表项。以下是activity_main.xml文件的代码:
```
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="LinearLayoutManager"
tools:listitem="@layout/item_course" />
</androidx.constraintlayout.widget.ConstraintLayout>
```
3. 创建一个自定义的Course类来表示课程和老师信息。以下是Course.java文件的代码:
```
public class Course {
private int id;
private String name;
private String teacher;
private int timeSlot;
public Course(int id, String name, String teacher, int timeSlot) {
this.id = id;
this.name = name;
this.teacher = teacher;
this.timeSlot = timeSlot;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getTeacher() {
return teacher;
}
public int getTimeSlot() {
return timeSlot;
}
}
```
4. 创建一个自定义的CourseAdapter类来管理RecyclerView中的课程项。以下是CourseAdapter.java文件的代码:
```
public class CourseAdapter extends RecyclerView.Adapter<CourseAdapter.CourseViewHolder> {
private List<Course> courses;
public CourseAdapter(List<Course> courses) {
this.courses = courses;
}
@NonNull
@Override
public CourseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_course, parent, false);
return new CourseViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull CourseViewHolder holder, int position) {
Course course = courses.get(position);
holder.courseName.setText(course.getName());
holder.courseTeacher.setText(course.getTeacher());
holder.courseTime.setText(String.format(Locale.getDefault(), "第%d节", course.getTimeSlot()));
}
@Override
public int getItemCount() {
return courses.size();
}
public static class CourseViewHolder extends RecyclerView.ViewHolder {
TextView courseName;
TextView courseTeacher;
TextView courseTime;
public CourseViewHolder(@NonNull View itemView) {
super(itemView);
courseName = itemView.findViewById(R.id.course_name);
courseTeacher = itemView.findViewById(R.id.course_teacher);
courseTime = itemView.findViewById(R.id.course_time);
}
}
}
```
5. 创建一个数据库帮助类来管理SQLite数据库。以下是DatabaseHelper.java文件的代码:
```
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "courses.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_COURSES = "courses";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_TEACHER = "teacher";
private static final String COLUMN_TIMESLOT = "time_slot";
private static final String CREATE_TABLE_COURSES = "CREATE TABLE " + TABLE_COURSES +
"(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_NAME + " TEXT, " +
COLUMN_TEACHER + " TEXT, " +
COLUMN_TIMESLOT + " INTEGER)";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_COURSES);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_COURSES);
onCreate(db);
}
public void addCourse(Course course) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, course.getName());
values.put(COLUMN_TEACHER, course.getTeacher());
values.put(COLUMN_TIMESLOT, course.getTimeSlot());
db.insert(TABLE_COURSES, null, values);
db.close();
}
public List<Course> getAllCourses() {
List<Course> courses = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_COURSES, null);
if (cursor.moveToFirst()) {
do {
int id = cursor.getInt(cursor.getColumnIndex(COLUMN_ID));
String name = cursor.getString(cursor.getColumnIndex(COLUMN_NAME));
String teacher = cursor.getString(cursor.getColumnIndex(COLUMN_TEACHER));
int timeSlot = cursor.getInt(cursor.getColumnIndex(COLUMN_TIMESLOT));
Course course = new Course(id, name, teacher, timeSlot);
courses.add(course);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return courses;
}
}
```
6. 在MainActivity.java文件中,我们将在RecyclerView中显示课程项,并使用AlertDialog来添加新的课程。以下是完整的MainActivity.java文件的代码:
```
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private CourseAdapter adapter;
private List<Course> courses;
private DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
databaseHelper = new DatabaseHelper(this);
courses = databaseHelper.getAllCourses();
adapter = new CourseAdapter(courses);
recyclerView.setAdapter(adapter);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showAddCourseDialog();
}
});
}
private void showAddCourseDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("添加课程");
View view = LayoutInflater.from(this).inflate(R.layout.dialog_add_course, null);
final EditText courseNameEditText = view.findViewById(R.id.course_name_edit_text);
final EditText courseTeacherEditText = view.findViewById(R.id.course_teacher_edit_text);
final Spinner courseTimeSpinner = view.findViewById(R.id.course_time_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.time_slots, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
courseTimeSpinner.setAdapter(adapter);
builder.setView(view)
.setPositiveButton("添加", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
String courseName = courseNameEditText.getText().toString();
String courseTeacher = courseTeacherEditText.getText().toString();
int courseTime = courseTimeSpinner.getSelectedItemPosition() + 1;
Course course = new Course(0, courseName, courseTeacher, courseTime);
databaseHelper.addCourse(course);
courses.clear();
courses.addAll(databaseHelper.getAllCourses());
adapter.notifyDataSetChanged();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
```
7. 最后,我们需要创建两个布局文件:item_course.xml和dialog_add_course.xml。以下是这两个文件的代码:
item_course.xml:
```
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/course_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/course_teacher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textSize="16sp" />
<TextView
android:id="@+id/course_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textSize="16sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
```
dialog_add_course.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="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/course_name_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="课程名称" />
<EditText
android:id="@+id/course_teacher_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="授课老师" />
<Spinner
android:id="@+id/course_time_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/time_slots" />
</LinearLayout>
```
这样,就完成了一个简单的安卓课表应用程序,它可以添加课程和老师以及第几节,并可以从SQLite数据库中检索和显示课程信息。
阅读全文