建立一个android工程,修改main.xml文件并编写程序代码,用SQLite编写一个学生信息管理应用,并实现插入、删除、更新、查找等方法。

时间: 2024-03-04 12:48:07 浏览: 19
好的,下面是一个简单的示例,用于演示如何使用SQLite实现学生信息管理应用。请按照以下步骤进行操作: 1. 创建一个Android工程,命名为"StudentManageApp"。 2. 在"res/layout"目录下创建一个名为"main.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"> <EditText android:id="@+id/editTextName" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="姓名"/> <EditText android:id="@+id/editTextAge" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="年龄"/> <EditText android:id="@+id/editTextGender" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="性别"/> <Button android:id="@+id/buttonInsert" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="添加"/> <Button android:id="@+id/buttonDelete" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="删除"/> <Button android:id="@+id/buttonUpdate" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="更新"/> <Button android:id="@+id/buttonSearch" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="查找"/> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> </LinearLayout> ``` 3. 在Java代码中实现学生信息管理功能。首先,在"app/build.gradle"文件中添加以下依赖: ``` dependencies { implementation 'androidx.recyclerview:recyclerview:1.1.0' implementation 'androidx.cardview:cardview:1.0.0' implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'com.google.android.material:material:1.1.0' implementation 'androidx.sqlite:sqlite:2.1.0' } ``` 然后,在MainActivity.java文件中添加以下代码: ``` public class MainActivity extends AppCompatActivity { private SQLiteDatabase db; private EditText editTextName, editTextAge, editTextGender; private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 打开或创建数据库 db = openOrCreateDatabase("student.db", Context.MODE_PRIVATE, null); // 创建学生表 db.execSQL("CREATE TABLE IF NOT EXISTS students (name TEXT, age INTEGER, gender TEXT)"); // 获取控件 editTextName = findViewById(R.id.editTextName); editTextAge = findViewById(R.id.editTextAge); editTextGender = findViewById(R.id.editTextGender); listView = findViewById(R.id.listView); // 设置ListView的适配器 Cursor cursor = db.rawQuery("SELECT rowid _id, * FROM students", null); SimpleCursorAdapter adapter = new SimpleCursorAdapter( this, android.R.layout.simple_list_item_1, cursor, new String[]{"name"}, new int[]{android.R.id.text1}, 0); listView.setAdapter(adapter); // 添加按钮点击事件 Button buttonInsert = findViewById(R.id.buttonInsert); buttonInsert.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name = editTextName.getText().toString(); int age = Integer.parseInt(editTextAge.getText().toString()); String gender = editTextGender.getText().toString(); ContentValues values = new ContentValues(); values.put("name", name); values.put("age", age); values.put("gender", gender); db.insert("students", null, values); refreshListView(); } }); // 删除按钮点击事件 Button buttonDelete = findViewById(R.id.buttonDelete); buttonDelete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name = editTextName.getText().toString(); db.delete("students", "name=?", new String[]{name}); refreshListView(); } }); // 更新按钮点击事件 Button buttonUpdate = findViewById(R.id.buttonUpdate); buttonUpdate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name = editTextName.getText().toString(); int age = Integer.parseInt(editTextAge.getText().toString()); String gender = editTextGender.getText().toString(); ContentValues values = new ContentValues(); values.put("age", age); values.put("gender", gender); db.update("students", values, "name=?", new String[]{name}); refreshListView(); } }); // 查找按钮点击事件 Button buttonSearch = findViewById(R.id.buttonSearch); buttonSearch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name = editTextName.getText().toString(); Cursor cursor = db.rawQuery("SELECT rowid _id, * FROM students WHERE name=?", new String[]{name}); SimpleCursorAdapter adapter = new SimpleCursorAdapter( MainActivity.this, android.R.layout.simple_list_item_1, cursor, new String[]{"name"}, new int[]{android.R.id.text1}, 0); listView.setAdapter(adapter); } }); } // 刷新ListView private void refreshListView() { Cursor cursor = db.rawQuery("SELECT rowid _id, * FROM students", null); SimpleCursorAdapter adapter = new SimpleCursorAdapter( this, android.R.layout.simple_list_item_1, cursor, new String[]{"name"}, new int[]{android.R.id.text1}, 0); listView.setAdapter(adapter); } } ``` 在代码中,我们首先创建或打开名为"student.db"的SQLite数据库,并创建名为"students"的学生信息表。然后,获取布局文件中的控件,并为添加、删除、更新和查找按钮添加点击事件。在添加、删除和更新操作中,我们使用ContentValues对象封装学生信息,并调用相应的数据库操作方法。在查找操作中,我们使用Cursor对象获取符合条件的学生信息,并更新ListView的适配器。最后,在onCreate方法中调用refreshListView方法刷新ListView。 4. 运行程序,测试功能是否正常。 希望这个示例对您有所帮助。

相关推荐

最新推荐

recommend-type

Android Studio如何获取SQLite数据并显示到ListView上

主要介绍了Android Studio获取SQLite数据并显示到ListView上,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

Python实现读取TXT文件数据并存进内置数据库SQLite3的方法

主要介绍了Python实现读取TXT文件数据并存进内置数据库SQLite3的方法,涉及Python针对txt文件的读取及sqlite3数据库的创建、插入、查询等相关操作技巧,需要的朋友可以参考下
recommend-type

六首页数字藏品NFT交易网React NextJS网站模板 六首页数字藏品nft交易网反应NextJS网站模板

六首页数字藏品NFT交易网React NextJS网站模板 六首页数字藏品nft交易网反应NextJS网站模板
recommend-type

wireshark安装教程入门

wireshark安装教程入门
recommend-type

基于C++负数据库的隐私保护在线医疗诊断系统

【作品名称】:基于C++负数据库的隐私保护在线医疗诊断系统 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【项目介绍】: 基于负数据库的隐私保护在线医疗诊断系统 NDBMedicalSystem 客户端及服务器端 本项目是在保护用户隐私的前提下,完成了对新冠肺炎、乳腺癌、眼疾等多种疾病的智能诊断。
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

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

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