在Androidstudio中设计一个简单学生选课系统并给出代码,在这个应用程序中有两个Activity界面,分别是对课程进行操作时的CourseActivity界面,以及对学生操作的PersonActivity界面。在CourseActivity界面可以对课程进行添加,删除以及跳转到PersonActivity功能,在PersonActivity中可以对person表数据进行增、删、改和查等操作,在这个工程中有两个实体类(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-01-06 20:04:36 浏览: 92
在Android Studio中开发一个Android应用项目是一个相对直接的过程,它涵盖了从创建项目到编写代码、设计界面、测试
在Android Studio中设计学生选课系统的步骤如下:
1. 创建新项目
在Android Studio中创建一个新项目,选择Empty Activity作为默认模板。
2. 定义实体类和扩展类
创建三个Java类:Person.java、Course.java和Relation.java,定义它们的属性和方法。其中,Person类包含id、myname和myclass三个属性,Course类包含id、name、obj和phone四个属性,Relation类包含studentId和courseId两个属性。
3. 创建数据库
在app包下创建一个名为DBOpenHelper.java的类,继承SQLiteOpenHelper类,并重写onCreate()和onUpgrade()方法。在onCreate()方法中创建student、course和relations三个表,并定义它们的字段。在onUpgrade()方法中更新表结构。
4. 创建CourseActivity和PersonActivity界面
在res/layout目录下创建两个XML布局文件:activity_course.xml和activity_person.xml,分别对应于CourseActivity和PersonActivity界面。在这两个布局文件中添加TextView、EditText、Button等控件,并设置它们的属性。
5. 编写Activity代码
创建CourseActivity和PersonActivity两个Java类,并继承AppCompatActivity类。在这两个类中,实现对数据库的增、删、改、查等操作,并将这些操作与界面上的控件进行绑定。在CourseActivity中,实现添加、删除课程和跳转到PersonActivity的功能,在PersonActivity中,实现对学生信息的增、删、改、查等操作。
最终的代码如下:
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;
}
// getter and setter methods
}
```
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;
}
// getter and setter methods
}
```
Relation.java
```
public class Relation {
private int studentId;
private int courseId;
public Relation(int studentId, int courseId) {
this.studentId = studentId;
this.courseId = courseId;
}
// getter and setter methods
}
```
DBOpenHelper.java
```
public class DBOpenHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "mydb.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_STUDENT = "student";
private static final String COL_STUDENT_ID = "id";
private static final String COL_STUDENT_NAME = "myname";
private static final String COL_STUDENT_CLASS = "myclass";
private static final String TABLE_COURSE = "course";
private static final String COL_COURSE_ID = "id";
private static final String COL_COURSE_NAME = "name";
private static final String COL_COURSE_OBJ = "obj";
private static final String COL_COURSE_PHONE = "phone";
private static final String TABLE_RELATIONS = "relations";
private static final String COL_RELATIONS_STUDENT_ID = "studentId";
private static final String COL_RELATIONS_COURSE_ID = "courseId";
private static final String CREATE_TABLE_STUDENT = "CREATE TABLE "
+ TABLE_STUDENT + "("
+ COL_STUDENT_ID + " INTEGER PRIMARY KEY,"
+ COL_STUDENT_NAME + " TEXT,"
+ COL_STUDENT_CLASS + " TEXT)";
private static final String CREATE_TABLE_COURSE = "CREATE TABLE "
+ TABLE_COURSE + "("
+ COL_COURSE_ID + " INTEGER PRIMARY KEY,"
+ COL_COURSE_NAME + " TEXT,"
+ COL_COURSE_OBJ + " TEXT,"
+ COL_COURSE_PHONE + " TEXT)";
private static final String CREATE_TABLE_RELATIONS = "CREATE TABLE "
+ TABLE_RELATIONS + "("
+ COL_RELATIONS_STUDENT_ID + " INTEGER,"
+ COL_RELATIONS_COURSE_ID + " INTEGER,"
+ "PRIMARY KEY (" + COL_RELATIONS_STUDENT_ID + ", " + COL_RELATIONS_COURSE_ID + "))";
public DBOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_STUDENT);
db.execSQL(CREATE_TABLE_COURSE);
db.execSQL(CREATE_TABLE_RELATIONS);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO: update table structure
}
}
```
CourseActivity.java
```
public class CourseActivity extends AppCompatActivity {
private EditText etId, etName, etObj, etPhone;
private Button btnAdd, btnDel, btnPerson;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course);
etId = findViewById(R.id.et_id);
etName = findViewById(R.id.et_name);
etObj = findViewById(R.id.et_obj);
etPhone = findViewById(R.id.et_phone);
btnAdd = findViewById(R.id.btn_add);
btnDel = findViewById(R.id.btn_del);
btnPerson = findViewById(R.id.btn_person);
DBOpenHelper dbHelper = new DBOpenHelper(this);
db = dbHelper.getWritableDatabase();
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String id = etId.getText().toString();
String name = etName.getText().toString();
String obj = etObj.getText().toString();
String phone = etPhone.getText().toString();
ContentValues values = new ContentValues();
values.put("id", id);
values.put("name", name);
values.put("obj", obj);
values.put("phone", phone);
db.insert("course", null, values);
}
});
btnDel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String id = etId.getText().toString();
db.delete("course", "id=?", new String[]{id});
}
});
btnPerson.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(CourseActivity.this, PersonActivity.class);
startActivity(intent);
}
});
}
}
```
PersonActivity.java
```
public class PersonActivity extends AppCompatActivity {
private EditText etId, etName, etClass;
private Button btnAdd, btnDel, btnUpdate, btnQuery;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_person);
etId = findViewById(R.id.et_id);
etName = findViewById(R.id.et_name);
etClass = findViewById(R.id.et_class);
btnAdd = findViewById(R.id.btn_add);
btnDel = findViewById(R.id.btn_del);
btnUpdate = findViewById(R.id.btn_update);
btnQuery = findViewById(R.id.btn_query);
DBOpenHelper dbHelper = new DBOpenHelper(this);
db = dbHelper.getWritableDatabase();
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String id = etId.getText().toString();
String name = etName.getText().toString();
String myclass = etClass.getText().toString();
ContentValues values = new ContentValues();
values.put("id", id);
values.put("myname", name);
values.put("myclass", myclass);
db.insert("student", null, values);
}
});
btnDel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String id = etId.getText().toString();
db.delete("student", "id=?", new String[]{id});
}
});
btnUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String id = etId.getText().toString();
String name = etName.getText().toString();
String myclass = etClass.getText().toString();
ContentValues values = new ContentValues();
values.put("myname", name);
values.put("myclass", myclass);
db.update("student", values, "id=?", new String[]{id});
}
});
btnQuery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String id = etId.getText().toString();
Cursor cursor = db.query("student", null, "id=?", new String[]{id}, null, null, null);
if (cursor.moveToFirst()) {
String name = cursor.getString(cursor.getColumnIndex("myname"));
String myclass = cursor.getString(cursor.getColumnIndex("myclass"));
etName.setText(name);
etClass.setText(myclass);
}
cursor.close();
}
});
}
}
```
阅读全文