写一个Android学生信息管理系统实例

时间: 2023-11-21 19:21:36 浏览: 50
好的,以下是一个简单的Android学生信息管理系统实例的代码: 首先,我们需要创建一个MainActivity来作为应用程序的主界面。在该Activity中,我们可以添加按钮来实现添加、删除、修改和查询学生信息的功能。 ```java public class MainActivity extends AppCompatActivity { private Button addBtn, deleteBtn, updateBtn, queryBtn; private EditText nameEt, ageEt, phoneEt, emailEt; private ListView studentListView; private SQLiteDatabase db; private StudentAdapter studentAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); addBtn = findViewById(R.id.add_btn); deleteBtn = findViewById(R.id.delete_btn); updateBtn = findViewById(R.id.update_btn); queryBtn = findViewById(R.id.query_btn); nameEt = findViewById(R.id.name_et); ageEt = findViewById(R.id.age_et); phoneEt = findViewById(R.id.phone_et); emailEt = findViewById(R.id.email_et); studentListView = findViewById(R.id.student_list_view); // 创建或打开数据库 db = openOrCreateDatabase("student.db", MODE_PRIVATE, null); db.execSQL("CREATE TABLE IF NOT EXISTS student(_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER, phone TEXT, email TEXT)"); // 设置ListView的适配器 studentAdapter = new StudentAdapter(this); studentListView.setAdapter(studentAdapter); // 添加按钮点击事件 addBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name = nameEt.getText().toString(); int age = Integer.parseInt(ageEt.getText().toString()); String phone = phoneEt.getText().toString(); String email = emailEt.getText().toString(); ContentValues cv = new ContentValues(); cv.put("name", name); cv.put("age", age); cv.put("phone", phone); cv.put("email", email); db.insert("student", null, cv); studentAdapter.notifyDataSetChanged(); } }); // 删除按钮点击事件 deleteBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name = nameEt.getText().toString(); db.delete("student", "name=?", new String[]{name}); studentAdapter.notifyDataSetChanged(); } }); // 修改按钮点击事件 updateBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name = nameEt.getText().toString(); int age = Integer.parseInt(ageEt.getText().toString()); String phone = phoneEt.getText().toString(); String email = emailEt.getText().toString(); ContentValues cv = new ContentValues(); cv.put("age", age); cv.put("phone", phone); cv.put("email", email); db.update("student", cv, "name=?", new String[]{name}); studentAdapter.notifyDataSetChanged(); } }); // 查询按钮点击事件 queryBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name = nameEt.getText().toString(); Cursor cursor = db.query("student", null, "name=?", new String[]{name}, null, null, "_id DESC"); if (cursor.moveToFirst()) { int age = cursor.getInt(cursor.getColumnIndex("age")); String phone = cursor.getString(cursor.getColumnIndex("phone")); String email = cursor.getString(cursor.getColumnIndex("email")); ageEt.setText(age + ""); phoneEt.setText(phone); emailEt.setText(email); } else { ageEt.setText(""); phoneEt.setText(""); emailEt.setText(""); Toast.makeText(MainActivity.this, "没有找到该学生信息", Toast.LENGTH_SHORT).show(); } cursor.close(); } }); } // 自定义ListView的适配器 private class StudentAdapter extends BaseAdapter { private Context context; private Cursor cursor; public StudentAdapter(Context context) { this.context = context; cursor = db.query("student", null, null, null, null, null, "_id DESC"); } @Override public int getCount() { return cursor.getCount(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.student_list_item, null); } TextView nameTv = convertView.findViewById(R.id.name_tv); TextView ageTv = convertView.findViewById(R.id.age_tv); TextView phoneTv = convertView.findViewById(R.id.phone_tv); TextView emailTv = convertView.findViewById(R.id.email_tv); cursor.moveToPosition(position); String name = cursor.getString(cursor.getColumnIndex("name")); int age = cursor.getInt(cursor.getColumnIndex("age")); String phone = cursor.getString(cursor.getColumnIndex("phone")); String email = cursor.getString(cursor.getColumnIndex("email")); nameTv.setText(name); ageTv.setText(age + ""); phoneTv.setText(phone); emailTv.setText(email); return convertView; } } } ``` 在布局文件中,我们需要添加一个ListView和一些EditText和Button来实现添加、删除、修改和查询学生信息的功能。 ```xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="16dp"> <EditText android:id="@+id/name_et" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="姓名" /> <EditText android:id="@+id/age_et" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="年龄" /> <EditText android:id="@+id/phone_et" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="电话" /> <EditText android:id="@+id/email_et" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="邮箱" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/name_et" android:layout_marginTop="16dp" android:layout_centerHorizontal="true" android:orientation="horizontal"> <Button android:id="@+id/add_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="添加" /> <Button android:id="@+id/delete_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="删除" /> <Button android:id="@+id/update_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="修改" /> <Button android:id="@+id/query_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查询" /> </LinearLayout> <ListView android:id="@+id/student_list_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/add_btn" /> </RelativeLayout> ``` 最后,我们需要创建一个用于显示每个学生信息的ListView的适配器StudentAdapter。在该适配器中,我们需要从数据库中读取学生信息,然后将它们显示在ListView中。 ```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="wrap_content" android:orientation="horizontal" android:padding="16dp"> <TextView android:id="@+id/name_tv" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="姓名" android:textSize="16sp" /> <TextView android:id="@+id/age_tv" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="年龄" android:textSize="16sp" /> <TextView android:id="@+id/phone_tv" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="电话" android:textSize="16sp" /> <TextView android:id="@+id/email_tv" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="邮箱" android:textSize="16sp" /> </LinearLayout> ``` 这样,我们就完成了一个简单的Android学生信息管理系统实例。

相关推荐

最新推荐

recommend-type

基于php+MySql实现学生信息管理系统实例

主要介绍了基于php+MySql实现学生信息管理系统实例,文中通过图片功能介绍的非常详细,文章末尾附带项目源码,需要的朋友可以参考下
recommend-type

java实现简单的学生信息管理系统代码实例

主要介绍了java实现简单的学生信息管理系统,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

Android activity堆栈及管理实例详解

主要介绍了Android activity堆栈及管理实例详解的相关资料,非常不错,具有参考借鉴价值,对android activity堆栈相关知识感兴趣的朋友一起学习吧
recommend-type

Android串口通信之串口读写实例

主要为大家详细介绍了Android串口通信之串口读写实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

android判断一个Activity是否处于栈顶的实例

下面小编就为大家分享一篇android判断一个Activity是否处于栈顶的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
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

解释minorization-maximization (MM) algorithm,并给出matlab代码编写的例子

Minorization-maximization (MM) algorithm是一种常用的优化算法,用于求解非凸问题或含有约束的优化问题。该算法的基本思想是通过构造一个凸下界函数来逼近原问题,然后通过求解凸下界函数的最优解来逼近原问题的最优解。具体步骤如下: 1. 初始化参数 $\theta_0$,设 $k=0$; 2. 构造一个凸下界函数 $Q(\theta|\theta_k)$,使其满足 $Q(\theta_k|\theta_k)=f(\theta_k)$; 3. 求解 $Q(\theta|\theta_k)$ 的最优值 $\theta_{k+1}=\arg\min_\theta Q(
recommend-type

JSBSim Reference Manual

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