在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 12:13:39 浏览: 20
这是一个简单的学生选课系统的代码和XML代码,其中包含两个Activity界面,一个CourseActivity界面和一个PersonActivity界面。 1. CourseActivity.java ``` import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import java.util.ArrayList; 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 = (ListView) findViewById(R.id.listView); adapter = new ArrayAdapter<Course>(this, android.R.layout.simple_list_item_1, courseList); listView.setAdapter(adapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent = new Intent(CourseActivity.this, PersonActivity.class); intent.putExtra("courseId", courseList.get(position).getId()); startActivity(intent); } }); } public void addCourse(View view) { // TODO: 添加课程 } public void deleteCourse(View view) { // TODO: 删除课程 } } ``` 2. activity_course.xml ``` <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" tools:context=".CourseActivity"> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/button_layout" /> <LinearLayout android:id="@+id/button_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal"> <Button android:id="@+id/add_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="addCourse" android:text="添加" /> <Button android:id="@+id/delete_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="deleteCourse" android:text="删除" /> </LinearLayout> </RelativeLayout> ``` 3. PersonActivity.java ``` import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.ListView; import android.widget.Toast; import java.util.ArrayList; public class PersonActivity extends AppCompatActivity { private ListView listView; private ArrayList<Person> personList = new ArrayList<>(); private ArrayAdapter<Person> adapter; private int courseId; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_person); listView = (ListView) findViewById(R.id.listView); adapter = new ArrayAdapter<Person>(this, android.R.layout.simple_list_item_1, personList); listView.setAdapter(adapter); courseId = getIntent().getIntExtra("courseId", 0); loadPersonList(); } public void addPerson(View view) { EditText idEditText = (EditText) findViewById(R.id.id_editText); EditText nameEditText = (EditText) findViewById(R.id.name_editText); EditText classEditText = (EditText) findViewById(R.id.class_editText); String id = idEditText.getText().toString(); String name = nameEditText.getText().toString(); String myclass = classEditText.getText().toString(); if (id.isEmpty() || name.isEmpty() || myclass.isEmpty()) { Toast.makeText(this, "请填写完整信息", Toast.LENGTH_SHORT).show(); return; } SQLiteDatabase db = new DBOpenHelper(this).getWritableDatabase(); ContentValues values = new ContentValues(); values.put("id", id); values.put("myname", name); values.put("myclass", myclass); db.insert("person", null, values); values.clear(); values.put("studentId", id); values.put("courseId", courseId); db.insert("relations", null, values); loadPersonList(); idEditText.setText(""); nameEditText.setText(""); classEditText.setText(""); } public void deletePerson(View view) { // TODO: 删除学生 } public void updatePerson(View view) { // TODO: 更新学生信息 } private void loadPersonList() { personList.clear(); SQLiteDatabase db = new DBOpenHelper(this).getReadableDatabase(); Cursor cursor = db.rawQuery("SELECT * FROM person, relations WHERE person.id = relations.studentId AND relations.courseId = " + courseId, null); while (cursor.moveToNext()) { Person person = new Person(); person.setId(cursor.getString(cursor.getColumnIndex("id"))); person.setName(cursor.getString(cursor.getColumnIndex("myname"))); person.setMyclass(cursor.getString(cursor.getColumnIndex("myclass"))); personList.add(person); } adapter.notifyDataSetChanged(); } } ``` 4. activity_person.xml ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <EditText android:id="@+id/id_editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="学号" /> <EditText android:id="@+id/name_editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="姓名" /> <EditText android:id="@+id/class_editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="班级" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/add_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="addPerson" android:text="添加" /> <Button android:id="@+id/delete_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="deletePerson" android:text="删除" /> <Button android:id="@+id/update_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="updatePerson" android:text="更新" /> </LinearLayout> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout> ``` 5. DBOpenHelper.java ``` import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DBOpenHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "course.db"; private static final int DATABASE_VERSION = 1; public DBOpenHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE IF NOT EXISTS course(" + "id INTEGER PRIMARY KEY," + "name TEXT," + "obj TEXT," + "phone TEXT)"); db.execSQL("CREATE TABLE IF NOT EXISTS person(" + "id TEXT PRIMARY KEY," + "myname TEXT," + "myclass TEXT)"); db.execSQL("CREATE TABLE IF NOT EXISTS relations(" + "id INTEGER PRIMARY KEY," + "studentId TEXT," + "courseId INTEGER)"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS course"); db.execSQL("DROP TABLE IF EXISTS person"); db.execSQL("DROP TABLE IF EXISTS relations"); onCreate(db); } } ```

相关推荐

最新推荐

recommend-type

android studio实现简单考试应用程序实例代码详解

主要介绍了android studio实现简单考试应用程序,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

Android中使用Kotlin实现一个简单的登录界面

Kotlin 是一种在 Java 虚拟机上运行的静态类型编程语言,被称之为 Android 世界的Swift,由 JetBrains 设计开发并开源。接下来本文通过实例代码给大家讲解Android中使用Kotlin实现一个简单的登录界面,一起看看吧
recommend-type

详解Android Studio实现用户登陆界面demo(xml实现)

主要介绍了详解Android Studio实现用户登陆界面demo,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

Android编程实现两个Activity相互切换而不使用onCreate()的方法

主要介绍了Android编程实现两个Activity相互切换而不使用onCreate()的方法,结合实例形式分析了多个Activity切换而不重新创建的操作技巧,需要的朋友可以参考下
recommend-type

在Android系统源码中预置APK的方法

今天小编就为大家分享一篇关于在Android系统源码中预置APK的方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB取整函数与Web开发的作用:round、fix、floor、ceil在Web开发中的应用

![MATLAB取整函数与Web开发的作用:round、fix、floor、ceil在Web开发中的应用](https://img-blog.csdnimg.cn/2020050917173284.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2thbmdqaWVsZWFybmluZw==,size_16,color_FFFFFF,t_70) # 1. MATLAB取整函数概述** MATLAB取整函数是一组强大的工具,用于对数值进行
recommend-type

我想做python的算法工程师,我应该学什么?学习的顺序是什么?网上有什么推荐的免费课程吗?回答具体精确一点不要太笼统

对于想要成为 Python 算法工程师的人来说,他们应该先从掌握 Python 的基础语法开始,然后学习数据结构和算法,并且要深入了解 Python 中的一些科学计算和数据处理库,比如 NumPy、Pandas 等。 学习的顺序可以是先学习基础语法和编程技巧,然后再学习数据结构和算法相关的知识,最后深入了解数据处理和科学计算相关的库。 对于免费课程的推荐,我建议你可以先去 Coursera、edX、Udacity 等网站上寻找相关课程,这些网站上有很多优质的 Python 编程和算法课程,你可以根据自己的需求和学习进度进行选择。此外,还可以考虑一些在线编程网站,如 HackerRank、L
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。