在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-03 18:11:19 浏览: 98
Android课设--学生选课系统-源代码+设计报告
5星 · 资源好评率100%
首先,我们需要创建三个实体类Person、Course和Relation。
Person.java:
```
public class Person {
private int id;
private String myname;
private String myclass;
public Person() {}
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() {}
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() {}
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:
```
public class DBOpenHelper extends SQLiteOpenHelper {
private final static String DATABASE_NAME = "myDatabase.db";
private final static int DATABASE_VERSION = 1;
public DBOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createStudentTable = "CREATE TABLE IF NOT EXISTS student (id INTEGER PRIMARY KEY AUTOINCREMENT, myname TEXT, myclass TEXT)";
String createCourseTable = "CREATE TABLE IF NOT EXISTS course (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, obj TEXT, phone TEXT)";
String createRelationTable = "CREATE TABLE IF NOT EXISTS relations (studentId INTEGER, courseId INTEGER, PRIMARY KEY (studentId, courseId), FOREIGN KEY (studentId) REFERENCES student(id), FOREIGN KEY (courseId) REFERENCES course(id))";
db.execSQL(createStudentTable);
db.execSQL(createCourseTable);
db.execSQL(createRelationTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS student");
db.execSQL("DROP TABLE IF EXISTS course");
db.execSQL("DROP TABLE IF EXISTS relations");
onCreate(db);
}
}
```
接下来,我们需要创建两个Activity类:CourseActivity和PersonActivity。
CourseActivity.java:
```
public class CourseActivity extends AppCompatActivity {
private ListView listView;
private ArrayList<Course> courseList = new ArrayList<>();
private ArrayAdapter<Course> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course);
listView = findViewById(R.id.listView);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, courseList);
listView.setAdapter(adapter);
Button addButton = findViewById(R.id.addButton);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(CourseActivity.this, AddCourseActivity.class);
startActivity(intent);
}
});
Button personButton = findViewById(R.id.personButton);
personButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(CourseActivity.this, PersonActivity.class);
startActivity(intent);
}
});
updateListView();
}
private void updateListView() {
courseList.clear();
DBOpenHelper dbHelper = new DBOpenHelper(this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = db.query("course", null, null, null, null, null, null);
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String obj = cursor.getString(cursor.getColumnIndex("obj"));
String phone = cursor.getString(cursor.getColumnIndex("phone"));
Course course = new Course(id, name, obj, phone);
courseList.add(course);
}
adapter.notifyDataSetChanged();
cursor.close();
db.close();
}
}
```
PersonActivity.java:
```
public class PersonActivity extends AppCompatActivity {
private ListView listView;
private ArrayList<Person> personList = new ArrayList<>();
private ArrayAdapter<Person> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_person);
listView = findViewById(R.id.listView);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, personList);
listView.setAdapter(adapter);
Button addButton = findViewById(R.id.addButton);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(PersonActivity.this, AddPersonActivity.class);
startActivity(intent);
}
});
updateListView();
}
private void updateListView() {
personList.clear();
DBOpenHelper dbHelper = new DBOpenHelper(this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = db.query("student", null, null, null, null, null, null);
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String myname = cursor.getString(cursor.getColumnIndex("myname"));
String myclass = cursor.getString(cursor.getColumnIndex("myclass"));
Person person = new Person(id, myname, myclass);
personList.add(person);
}
adapter.notifyDataSetChanged();
cursor.close();
db.close();
}
}
```
在上面的代码中,我们使用了ListView来显示数据库中的数据。我们使用了DBOpenHelper类来获取可写的SQLiteDatabase对象,然后使用Cursor对象来遍历查询结果。
接下来,我们需要创建添加课程和添加学生的Activity类:AddCourseActivity和AddPersonActivity。
AddCourseActivity.java:
```
public class AddCourseActivity extends AppCompatActivity {
private EditText nameEditText;
private EditText objEditText;
private EditText phoneEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_course);
nameEditText = findViewById(R.id.nameEditText);
objEditText = findViewById(R.id.objEditText);
phoneEditText = findViewById(R.id.phoneEditText);
Button addButton = findViewById(R.id.addButton);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = nameEditText.getText().toString();
String obj = objEditText.getText().toString();
String phone = phoneEditText.getText().toString();
DBOpenHelper dbHelper = new DBOpenHelper(AddCourseActivity.this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", name);
values.put("obj", obj);
values.put("phone", phone);
db.insert("course", null, values);
db.close();
finish();
}
});
}
}
```
AddPersonActivity.java:
```
public class AddPersonActivity extends AppCompatActivity {
private EditText mynameEditText;
private EditText myclassEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_person);
mynameEditText = findViewById(R.id.mynameEditText);
myclassEditText = findViewById(R.id.myclassEditText);
Button addButton = findViewById(R.id.addButton);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String myname = mynameEditText.getText().toString();
String myclass = myclassEditText.getText().toString();
DBOpenHelper dbHelper = new DBOpenHelper(AddPersonActivity.this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("myname", myname);
values.put("myclass", myclass);
db.insert("student", null, values);
db.close();
finish();
}
});
}
}
```
最后,我们需要在AndroidManifest.xml中声明这两个Activity:
```
<activity android:name=".CourseActivity" />
<activity android:name=".PersonActivity" />
<activity android:name=".AddCourseActivity" />
<activity android:name=".AddPersonActivity" />
```
现在,我们已经完成了一个简单的学生选课系统的开发。
阅读全文