在Androidstudio中设计一个简单学生选课系统并给出代码以及XML代码,在这个应用程序中有两个Activity界面,分别是对课程进行操作时的CourseActivity界面,以及对学生操作的PersonActivity界面。在CourseActivity界面可以对课程进行添加,删除以及跳转到PersonActivity功能和通过ListView将所有课程显示在页面上,在PersonActivity中可以对person表数据进行增、删、改等操作和通过ListView将所有学生信息显示在页面上,在这个工程中有两个实体类(Person.java、Course.java)和一个扩展类(Relation.java)但是不用给出这三个类的代码,Person.java类定义了学生的属性信息,Course.java类定义了课程的属性对象,而Relation.java类是学生和课程之间的关系类实现了学生和课程之间的多对多存储关系。三个类的属性定义如下,(1)person类的定义为:id(学生号),myname(学生名),myclass(学生班级),(2)Course类:id(课程号)、name(课程名)、obj(上课对象)、phone(班长电话),(3)Relation类:studentId(学生id)、courseId(课程Id)。定义三个数据表,表结构分别为:student表、course表、relations表,通过定义DBOpenHelper类继承SQLiteOpenHelper类来创建数据库,使用SQLite存储数据
时间: 2024-02-01 22:16:41 浏览: 150
这里给出一个简单的实现,包括CourseActivity和PersonActivity界面的代码以及相应的XML布局文件。需要注意的是,这里只给出了对于课程和学生的基本操作,没有实现多对多关系的存储和操作。具体实现可以参考Relation类的定义,使用join操作等。
CourseActivity.java
```java
public class CourseActivity extends AppCompatActivity {
private ListView courseList;
private List<Course> courses;
private CourseAdapter courseAdapter;
private EditText courseIdEditText;
private EditText courseNameEditText;
private EditText courseObjEditText;
private EditText coursePhoneEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course);
// 初始化ListView
courseList = findViewById(R.id.course_list);
courses = new ArrayList<>();
courseAdapter = new CourseAdapter(this, courses);
courseList.setAdapter(courseAdapter);
// 初始化EditText
courseIdEditText = findViewById(R.id.course_id_edit_text);
courseNameEditText = findViewById(R.id.course_name_edit_text);
courseObjEditText = findViewById(R.id.course_obj_edit_text);
coursePhoneEditText = findViewById(R.id.course_phone_edit_text);
// 从数据库中读取数据并显示
refreshCourseList();
// 设置ListView的点击事件,可以跳转到PersonActivity界面查看选修该课程的学生
courseList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Course course = courses.get(position);
Intent intent = new Intent(CourseActivity.this, PersonActivity.class);
intent.putExtra("courseId", course.getId());
startActivity(intent);
}
});
}
// 刷新ListView
private void refreshCourseList() {
courses.clear();
courses.addAll(CourseDao.getAllCourses(this));
courseAdapter.notifyDataSetChanged();
}
// 添加课程
public void addCourse(View view) {
String id = courseIdEditText.getText().toString();
String name = courseNameEditText.getText().toString();
String obj = courseObjEditText.getText().toString();
String phone = coursePhoneEditText.getText().toString();
// 检查输入是否为空
if (TextUtils.isEmpty(id) || TextUtils.isEmpty(name) || TextUtils.isEmpty(obj) || TextUtils.isEmpty(phone)) {
Toast.makeText(this, "输入不能为空!", Toast.LENGTH_SHORT).show();
return;
}
// 创建Course对象并插入到数据库中
Course course = new Course(id, name, obj, phone);
CourseDao.addCourse(this, course);
// 刷新ListView
refreshCourseList();
// 清空EditText
courseIdEditText.setText("");
courseNameEditText.setText("");
courseObjEditText.setText("");
coursePhoneEditText.setText("");
Toast.makeText(this, "添加成功!", Toast.LENGTH_SHORT).show();
}
// 删除课程
public void deleteCourse(View view) {
// 获取选中的课程
SparseBooleanArray selectedItems = courseAdapter.getSelectedItems();
for (int i = selectedItems.size() - 1; i >= 0; i--) {
if (selectedItems.valueAt(i)) {
Course course = courses.get(selectedItems.keyAt(i));
CourseDao.deleteCourse(this, course.getId());
}
}
// 刷新ListView
refreshCourseList();
courseAdapter.clearSelection();
}
}
```
PersonActivity.java
```java
public class PersonActivity extends AppCompatActivity {
private ListView personList;
private List<Person> persons;
private PersonAdapter personAdapter;
private EditText personIdEditText;
private EditText personNameEditText;
private EditText personClassEditText;
private String courseId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_person);
// 获取传递过来的课程id
Intent intent = getIntent();
courseId = intent.getStringExtra("courseId");
// 初始化ListView
personList = findViewById(R.id.person_list);
persons = new ArrayList<>();
personAdapter = new PersonAdapter(this, persons);
personList.setAdapter(personAdapter);
// 初始化EditText
personIdEditText = findViewById(R.id.person_id_edit_text);
personNameEditText = findViewById(R.id.person_name_edit_text);
personClassEditText = findViewById(R.id.person_class_edit_text);
// 从数据库中读取数据并显示
refreshPersonList();
// 设置ListView的长按事件,可以删除选中的学生
personList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Person person = persons.get(position);
PersonDao.deletePerson(PersonActivity.this, person.getId());
refreshPersonList();
return true;
}
});
}
// 刷新ListView
private void refreshPersonList() {
persons.clear();
persons.addAll(PersonDao.getPersonsByCourseId(this, courseId));
personAdapter.notifyDataSetChanged();
}
// 添加学生
public void addPerson(View view) {
String id = personIdEditText.getText().toString();
String name = personNameEditText.getText().toString();
String myclass = personClassEditText.getText().toString();
// 检查输入是否为空
if (TextUtils.isEmpty(id) || TextUtils.isEmpty(name) || TextUtils.isEmpty(myclass)) {
Toast.makeText(this, "输入不能为空!", Toast.LENGTH_SHORT).show();
return;
}
// 创建Person对象并插入到数据库中
Person person = new Person(id, name, myclass);
PersonDao.addPerson(this, person);
// 创建Relation对象并插入到数据库中
Relation relation = new Relation(id, courseId);
RelationDao.addRelation(this, relation);
// 刷新ListView
refreshPersonList();
// 清空EditText
personIdEditText.setText("");
personNameEditText.setText("");
personClassEditText.setText("");
Toast.makeText(this, "添加成功!", Toast.LENGTH_SHORT).show();
}
// 修改学生
public void updatePerson(View view) {
// 获取选中的学生
SparseBooleanArray selectedItems = personAdapter.getSelectedItems();
for (int i = selectedItems.size() - 1; i >= 0; i--) {
if (selectedItems.valueAt(i)) {
Person person = persons.get(selectedItems.keyAt(i));
// 更新数据库中的Person对象
person.setName(personNameEditText.getText().toString());
person.setMyclass(personClassEditText.getText().toString());
PersonDao.updatePerson(this, person.getId(), person);
// 刷新ListView
refreshPersonList();
}
}
personAdapter.clearSelection();
}
}
```
activity_course.xml
```xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="课程列表"
android:textSize="20sp"
android:textStyle="bold" />
<ListView
android:id="@+id/course_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:choiceMode="multipleChoice" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="16dp">
<EditText
android:id="@+id/course_id_edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="课程号" />
<EditText
android:id="@+id/course_name_edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="课程名" />
<EditText
android:id="@+id/course_obj_edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="上课对象" />
<EditText
android:id="@+id/course_phone_edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="班长电话" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="addCourse"
android:text="添加" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="deleteCourse"
android:text="删除" />
</LinearLayout>
</LinearLayout>
```
activity_person.xml
```xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="学生列表"
android:textSize="20sp"
android:textStyle="bold" />
<ListView
android:id="@+id/person_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:choiceMode="multipleChoice" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="16dp">
<EditText
android:id="@+id/person_id_edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="学号" />
<EditText
android:id="@+id/person_name_edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="姓名" />
<EditText
android:id="@+id/person_class_edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="班级" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="addPerson"
android:text="添加" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="updatePerson"
android:text="修改" />
</LinearLayout>
</LinearLayout>
```
阅读全文