在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 13:16:57 浏览: 67
XML布局代码如下:
CourseActivity.xml
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn_add_course"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加课程"
android:layout_gravity="center_horizontal"/>
<ListView
android:id="@+id/course_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"/>
</LinearLayout>
```
PersonActivity.xml
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn_add_person"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加学生"
android:layout_gravity="center_horizontal"/>
<ListView
android:id="@+id/person_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"/>
</LinearLayout>
```
Java代码如下:
Person.java
```
public class Person {
private int id;
private String myname;
private String myclass;
public Person(int id, String myname, String myclass) {
this.id = id;
this.myname = myname;
this.myclass = myclass;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getMyname() {
return myname;
}
public void setMyname(String myname) {
this.myname = myname;
}
public String getMyclass() {
return myclass;
}
public void setMyclass(String myclass) {
this.myclass = myclass;
}
}
```
Course.java
```
public class Course {
private int id;
private String name;
private String obj;
private String phone;
public Course(int id, String name, String obj, String phone) {
this.id = id;
this.name = name;
this.obj = obj;
this.phone = phone;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getObj() {
return obj;
}
public void setObj(String obj) {
this.obj = obj;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
```
Relation.java
```
public class Relation {
private int studentId;
private int courseId;
public Relation(int studentId, int courseId) {
this.studentId = studentId;
this.courseId = courseId;
}
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public int getCourseId() {
return courseId;
}
public void setCourseId(int courseId) {
this.courseId = courseId;
}
}
```
DBOpenHelper.java
```
public class DBOpenHelper extends SQLiteOpenHelper {
public static final String DB_NAME = "student_course.db";
public static final String STUDENT_TABLE = "student";
public static final String STUDENT_ID = "_id";
public static final String STUDENT_NAME = "myname";
public static final String STUDENT_CLASS = "myclass";
public static final String COURSE_TABLE = "course";
public static final String COURSE_ID = "_id";
public static final String COURSE_NAME = "name";
public static final String COURSE_OBJ = "obj";
public static final String COURSE_PHONE = "phone";
public static final String RELATIONS_TABLE = "relations";
public static final String RELATIONS_STUDENT_ID = "studentId";
public static final String RELATIONS_COURSE_ID = "courseId";
public DBOpenHelper(Context context) {
super(context, DB_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_STUDENT_TABLE = "create table " + STUDENT_TABLE + " ("
+ STUDENT_ID + " integer primary key autoincrement,"
+ STUDENT_NAME + " text,"
+ STUDENT_CLASS + " text)";
db.execSQL(CREATE_STUDENT_TABLE);
String CREATE_COURSE_TABLE = "create table " + COURSE_TABLE + " ("
+ COURSE_ID + " integer primary key autoincrement,"
+ COURSE_NAME + " text,"
+ COURSE_OBJ + " text,"
+ COURSE_PHONE + " text)";
db.execSQL(CREATE_COURSE_TABLE);
String CREATE_RELATIONS_TABLE = "create table " + RELATIONS_TABLE + " ("
+ RELATIONS_STUDENT_ID + " integer,"
+ RELATIONS_COURSE_ID + " integer,"
+ "primary key(" + RELATIONS_STUDENT_ID + "," + RELATIONS_COURSE_ID + "))";
db.execSQL(CREATE_RELATIONS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists " + STUDENT_TABLE);
db.execSQL("drop table if exists " + COURSE_TABLE);
db.execSQL("drop table if exists " + RELATIONS_TABLE);
onCreate(db);
}
}
```
CourseActivity.java
```
public class CourseActivity extends AppCompatActivity {
private Button btnAddCourse;
private ListView courseListView;
private List<Course> courseList = new ArrayList<>();
private ArrayAdapter<Course> arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course);
btnAddCourse = findViewById(R.id.btn_add_course);
courseListView = findViewById(R.id.course_list_view);
arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, courseList);
courseListView.setAdapter(arrayAdapter);
btnAddCourse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 添加课程
// TODO
}
});
courseListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// 跳转到PersonActivity界面
// TODO
}
});
}
}
```
PersonActivity.java
```
public class PersonActivity extends AppCompatActivity {
private Button btnAddPerson;
private ListView personListView;
private List<Person> personList = new ArrayList<>();
private ArrayAdapter<Person> arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_person);
btnAddPerson = findViewById(R.id.btn_add_person);
personListView = findViewById(R.id.person_list_view);
arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, personList);
personListView.setAdapter(arrayAdapter);
btnAddPerson.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 添加学生
// TODO
}
});
personListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// 修改或删除学生信息
// TODO
}
});
}
}
```
阅读全文