在Androidstudio中设计一个简单学生选课系统并给出代码,在这个应用程序中有两个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-01-06 19:04:36 浏览: 80
首先,我们需要在Android Studio中创建一个新项目。然后,在项目中创建三个Java类:Person.java、Course.java、Relation.java,并且创建一个名为DBOpenHelper.java的SQLiteOpenHelper类。
Person.java类定义:
```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类定义:
```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类定义:
```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类定义:
```java
public class DBOpenHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "select_course.db";// 数据库名
private static final int DATABASE_VERSION = 1;// 数据库版本号
public static final String TABLE_STUDENT = "student";// 学生表
public static final String TABLE_COURSE = "course";// 课程表
public static final String TABLE_RELATIONS = "relations";// 学生与课程关系表
// 学生表字段
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_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_STUDENT_ID = "studentId";
public static final String RELATIONS_COURSE_ID = "courseId";
// 创建学生表
private static final String CREATE_TABLE_STUDENT = "create table "
+ TABLE_STUDENT + "(" + STUDENT_ID
+ " integer primary key autoincrement, " + STUDENT_NAME
+ " varchar(20), " + STUDENT_CLASS + " varchar(20))";
// 创建课程表
private static final String CREATE_TABLE_COURSE = "create table "
+ TABLE_COURSE + "(" + COURSE_ID
+ " integer primary key autoincrement, " + COURSE_NAME
+ " varchar(20), " + COURSE_OBJ + " varchar(20), "
+ COURSE_PHONE + " varchar(20))";
// 创建学生与课程关系表
private static final String CREATE_TABLE_RELATIONS = "create table "
+ TABLE_RELATIONS + "(" + RELATIONS_STUDENT_ID
+ " integer, " + RELATIONS_COURSE_ID + " integer)";
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) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_STUDENT);// 删除学生表
db.execSQL("DROP TABLE IF EXISTS " + TABLE_COURSE);// 删除课程表
db.execSQL("DROP TABLE IF EXISTS " + TABLE_RELATIONS);// 删除学生与课程关系表
onCreate(db);
}
}
```
接下来,我们需要创建两个Activity:CourseActivity和PersonActivity。
CourseActivity.java代码:
```java
public class CourseActivity extends AppCompatActivity {
private ListView listView;
private SQLiteDatabase database;
private DBOpenHelper dbOpenHelper;
private List<Course> courseList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course);
listView = findViewById(R.id.course_list_view);
dbOpenHelper = new DBOpenHelper(this);
database = dbOpenHelper.getWritableDatabase();
Button addButton = findViewById(R.id.add_course_button);
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.person_button);
personButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(CourseActivity.this, PersonActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onResume() {
super.onResume();
courseList.clear();
Cursor cursor = database.query(DBOpenHelper.TABLE_COURSE, null, null, null, null, null, null);
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex(DBOpenHelper.COURSE_ID));
String name = cursor.getString(cursor.getColumnIndex(DBOpenHelper.COURSE_NAME));
String obj = cursor.getString(cursor.getColumnIndex(DBOpenHelper.COURSE_OBJ));
String phone = cursor.getString(cursor.getColumnIndex(DBOpenHelper.COURSE_PHONE));
Course course = new Course(id, name, obj, phone);
courseList.add(course);
}
cursor.close();
CourseAdapter adapter = new CourseAdapter(CourseActivity.this, courseList);
listView.setAdapter(adapter);
}
private class CourseAdapter extends BaseAdapter {
private Context context;
private List<Course> list;
public CourseAdapter(Context context, List<Course> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(R.layout.course_item, null);
TextView nameView = view.findViewById(R.id.course_name_view);
TextView objView = view.findViewById(R.id.course_obj_view);
TextView phoneView = view.findViewById(R.id.course_phone_view);
Course course = list.get(position);
nameView.setText(course.getName());
objView.setText(course.getObj());
phoneView.setText(course.getPhone());
return view;
}
}
}
```
PersonActivity.java代码:
```java
public class PersonActivity extends AppCompatActivity {
private ListView listView;
private SQLiteDatabase database;
private DBOpenHelper dbOpenHelper;
private List<Person> personList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_person);
listView = findViewById(R.id.person_list_view);
dbOpenHelper = new DBOpenHelper(this);
database = dbOpenHelper.getWritableDatabase();
Button addButton = findViewById(R.id.add_person_button);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(PersonActivity.this, AddPersonActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onResume() {
super.onResume();
personList.clear();
Cursor cursor = database.query(DBOpenHelper.TABLE_STUDENT, null, null, null, null, null, null);
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex(DBOpenHelper.STUDENT_ID));
String name = cursor.getString(cursor.getColumnIndex(DBOpenHelper.STUDENT_NAME));
String myclass = cursor.getString(cursor.getColumnIndex(DBOpenHelper.STUDENT_CLASS));
Person person = new Person(id, name, myclass);
personList.add(person);
}
cursor.close();
PersonAdapter adapter = new PersonAdapter(PersonActivity.this, personList);
listView.setAdapter(adapter);
}
private class PersonAdapter extends BaseAdapter {
private Context context;
private List<Person> list;
public PersonAdapter(Context context, List<Person> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(R.layout.person_item, null);
TextView nameView = view.findViewById(R.id.person_name_view);
TextView classView = view.findViewById(R.id.person_class_view);
Person person = list.get(position);
nameView.setText(person.getMyname());
classView.setText(person.getMyclass());
return view;
}
}
}
```
接下来,我们需要创建两个添加数据的Activity:AddCourseActivity和AddPersonActivity。
AddCourseActivity.java代码:
```java
public class AddCourseActivity extends AppCompatActivity {
private SQLiteDatabase database;
private DBOpenHelper dbOpenHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_course);
dbOpenHelper = new DBOpenHelper(this);
database = dbOpenHelper.getWritableDatabase();
Button addButton = findViewById(R.id.add_course_button);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText nameEdit = findViewById(R.id.course_name_edit);
EditText objEdit = findViewById(R.id.course_obj_edit);
EditText phoneEdit = findViewById(R.id.course_phone_edit);
String name = nameEdit.getText().toString().trim();
String obj = objEdit.getText().toString().trim();
String phone = phoneEdit.getText().toString().trim();
ContentValues values = new ContentValues();
values.put(DBOpenHelper.COURSE_NAME, name);
values.put(DBOpenHelper.COURSE_OBJ, obj);
values.put(DBOpenHelper.COURSE_PHONE, phone);
database.insert(DBOpenHelper.TABLE_COURSE, null, values);
finish();
}
});
}
}
```
AddPersonActivity.java代码:
```java
public class AddPersonActivity extends AppCompatActivity {
private SQLiteDatabase database;
private DBOpenHelper dbOpenHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_person);
dbOpenHelper = new DBOpenHelper(this);
database = dbOpenHelper.getWritableDatabase();
Button addButton = findViewById(R.id.add_person_button);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText nameEdit = findViewById(R.id.person_name_edit);
EditText classEdit = findViewById(R.id.person_class_edit);
String name = nameEdit.getText().toString().trim();
String myclass = classEdit.getText().toString().trim();
ContentValues values = new ContentValues();
values.put(DBOpenHelper.STUDENT_NAME, name);
values.put(DBOpenHelper.STUDENT_CLASS, myclass);
database.insert(DBOpenHelper.TABLE_STUDENT, null, values);
finish();
}
});
}
}
```
最后,我们需要在AndroidManifest.xml文件中注册这些Activity:
```xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.selectcourse">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".AddCourseActivity"></activity>
<activity android:name=".AddPersonActivity"></activity>
<activity android:name=".CourseActivity"></activity>
<activity android:name=".PersonActivity"></activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
```
以上就是一个简单的学生选课系统的代码实现,希望能对您有所帮助。
阅读全文