基于轻量化SQLiteDatabase数据库存储,app更新,数据不会丢失,但卸载了重装就会数据丢失!
代码参考学生信息管理系统Kotlin实现简单的学生信息管理系统_振华OPPO的博客-CSDN博客,懒得改student为patient。
查询的主键key是住院号(唯一,不重复)。
住院号为空时,查询即是显示全部患者数据。
对有些字段,如住院号,电话,年份,年龄给予输入长度MaxLength限制。
对查询结果进行横向和纵向的滑动显示,模仿表格效果。
将已有的患者数据导出到Excel文件,卸载软件之前这个“导出”还是非常重要的,否则数据丢失!
本Demo下载 密码:efbm
目录
效果
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="乳腺癌患者科研信息管理系统"
android:gravity="center"
android:textSize="20dp"
android:background="#64E388"></TextView>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="350dp"
android:background="#FFD491">
<EditText
android:id="@+id/et_id"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@drawable/et_selector"
android:hint="住院号*"
android:inputType="number"
android:maxLength="7"
android:textColor="#AA0318"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="@+id/et_name"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@drawable/et_selector"
android:hint="姓名"
android:inputType="text"
android:maxLength="4"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="@+id/et_phone"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@drawable/et_selector"
android:hint="电话"
android:inputType="phone"
android:maxLength="11"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.17" />
<EditText
android:id="@+id/et_diagnoseyear"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@drawable/et_selector"
android:hint="确诊年份"
android:inputType="number"
android:maxLength="4"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.17" />
<EditText
android:id="@+id/et_age"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@drawable/et_selector"
android:hint="发病年龄"
android:inputType="number"
android:maxLength="2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.17" />
<EditText
android:id="@+id/et_TNM"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@drawable/et_selector"
android:hint="TNM分期"
android:inputType="text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.34" />
<EditText
android:id="@+id/et_subtype"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@drawable/et_selector"
android:hint="分子分型"
android:inputType="text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.34" />
<EditText
android:id="@+id/et_treatment"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="20dp"
android:background="@drawable/et_selector"
android:hint="请输入治疗方案"
android:inputType="text"
android:maxLength="100"
android:paddingLeft="10dp"
android:paddingRight="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.68" />
<EditText
android:id="@+id/et_neochemotherapy"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@drawable/et_selector"
android:hint="是否新辅化疗"
android:inputType="text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.34" />
<EditText
android:id="@+id/et_bPCR"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@drawable/et_selector"
android:hint="是否breastPCR"
android:inputType="text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.49" />
<EditText
android:id="@+id/et_tPCR"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@drawable/et_selector"
android:hint="是否totalPCR"
android:inputType="text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.49" />
<EditText
android:id="@+id/et_re"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@drawable/et_selector"
android:hint="是否复发转移"
android:inputType="text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.86" />
<EditText
android:id="@+id/et_bilateral"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@drawable/et_selector"
android:hint="是否双侧乳癌"
android:inputType="text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.86" />
<EditText
android:id="@+id/et_familyhistory"
android:layout_width="172dp"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@drawable/et_selector"
android:gravity="center"
android:hint="乳腺癌家族史"
android:inputType="text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Id"
app:layout_constraintBottom_toTopOf="@+id/et_id"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.05"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Patient"
app:layout_constraintBottom_toTopOf="@+id/et_name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.792"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="@+id/textView18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tel"
app:layout_constraintBottom_toTopOf="@+id/et_phone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.051"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Year"
app:layout_constraintBottom_toTopOf="@+id/et_diagnoseyear"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.493"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age"
app:layout_constraintBottom_toTopOf="@+id/et_age"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.805"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TNM"
app:layout_constraintBottom_toTopOf="@+id/et_TNM"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.052"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Subtype"
app:layout_constraintBottom_toTopOf="@+id/et_subtype"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Neochemotherapy"
app:layout_constraintBottom_toTopOf="@+id/et_neochemotherapy"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.945"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.976" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bPCR"
app:layout_constraintBottom_toTopOf="@+id/et_bPCR"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.053"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tPCR"
app:layout_constraintBottom_toTopOf="@+id/et_tPCR"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.686"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="@+id/textView17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Treatment"
app:layout_constraintBottom_toTopOf="@+id/et_treatment"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bilateral"
app:layout_constraintBottom_toTopOf="@+id/et_bilateral"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.055"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Recur/Meta"
app:layout_constraintBottom_toTopOf="@+id/et_re"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.774"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BC family history"
app:layout_constraintBottom_toTopOf="@+id/et_familyhistory"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.491"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_insert"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@drawable/btn_selector"
android:gravity="center"
android:text="增加"
android:textColor="@color/black"
android:textSize="20sp" />
<Button
android:id="@+id/btn_delete"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@drawable/btn_selector"
android:gravity="center"
android:text="删除"
android:textColor="@color/black"
android:textSize="20sp" />
<Button
android:id="@+id/btn_update"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@drawable/btn_selector"
android:gravity="center"
android:text="修改"
android:textColor="@color/black"
android:textSize="20sp" />
<Button
android:id="@+id/btn_query"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@drawable/btn_selector"
android:gravity="center"
android:text="查询"
android:textColor="@color/black"
android:textSize="20sp" />
<Button
android:id="@+id/btn_dbtoexcel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@drawable/btn_selector"
android:gravity="center"
android:text="导出"
android:textColor="@color/black"
android:textSize="20sp"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@color/black" />
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#89B5F8">
<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="住院号"
android:textSize="12sp" />
<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="姓名"
android:textSize="14sp" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="电话"
android:textSize="12sp" />
<TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="诊断年份"
android:textSize="12sp" />
<TextView
android:layout_width="30dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="发病年龄"
android:textSize="12sp" />
<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="TNM分期"
android:textSize="12sp" />
<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="分子分型"
android:textSize="12sp" />
<TextView
android:layout_width="300dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="治疗方案"
android:textSize="12sp" />
<TextView
android:layout_width="30dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="新辅"
android:textSize="12sp" />
<TextView
android:layout_width="40dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="bPCR"
android:textSize="12sp" />
<TextView
android:layout_width="40dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="tPCR"
android:textSize="12sp" />
<TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="复发转移"
android:textSize="12sp" />
<TextView
android:layout_width="30dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="双乳"
android:textSize="12sp" />
<TextView
android:layout_width="60dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="家族史"
android:textSize="12sp" />
</LinearLayout>
<ListView
android:id="@+id/lv_stu"
android:layout_width="1000dp"
android:layout_height="wrap_content" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
item_student.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">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="#E4C5DD">
<TextView
android:id="@+id/tv_id"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="住院号"
android:textColor="#071D95"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_name"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingRight="5dp"
android:text="姓名"
android:textColor="#7A280F"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_phone"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingRight="5dp"
android:text="电话"
android:textColor="@color/black"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_diagnoseyear"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingRight="5dp"
android:text="诊断年"
android:textColor="@color/black"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_age"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingRight="5dp"
android:text="发病年龄"
android:textColor="@color/black"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_TNM"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingRight="5dp"
android:text="T1N0M0"
android:textColor="@color/black"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_subtype"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingRight="5dp"
android:text="T1N0M0"
android:textColor="@color/black"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_treatment"
android:layout_width="300dp"
android:layout_height="50dp"
android:gravity="left"
android:paddingRight="5dp"
android:text="治疗方案"
android:textColor="@color/black"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_neochemotherapy"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingRight="5dp"
android:text="新辅"
android:textColor="@color/black"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_bPCR"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingRight="5dp"
android:text="breastPCR"
android:textColor="@color/black"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_tPCR"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingRight="5dp"
android:text="totalPCR"
android:textColor="@color/black"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_re"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingRight="5dp"
android:text="复发转移"
android:textColor="@color/black"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_bilateral"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingRight="5dp"
android:text="双乳"
android:textColor="@color/black"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_familyhistory"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingRight="5dp"
android:text="家族史"
android:textColor="@color/black"
android:textSize="12sp"
android:textStyle="bold" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
MainActivity.kt
package com.android.smis
import android.annotation.SuppressLint
import android.database.sqlite.SQLiteDatabase
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.android.smis.databinding.ActivityMainBinding
import jxl.Workbook
import jxl.write.Label
import java.io.File
import java.util.*
class MainActivity : AppCompatActivity(), View.OnClickListener {
lateinit var binding: ActivityMainBinding
val studentDao = StudentDao(this)
var studentList = ArrayList<Student>()
lateinit var adapter: StudentAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
studentDao.openDB()
initView()
}
fun initView() {
binding.btnInsert.setOnClickListener(this)
binding.btnDelete.setOnClickListener(this)
binding.btnUpdate.setOnClickListener(this)
binding.btnQuery.setOnClickListener(this)
binding.btnDbtoexcel.setOnClickListener(this)
}
override fun onClick(p0: View?) {
var stuId = binding.etId.text.toString()
var stuName = binding.etName.text.toString()
var stuPhone = binding.etPhone.text.toString()
var stuDiagnoseyear = binding.etDiagnoseyear.text.toString()
var stuAge = binding.etAge.text.toString()
var stuTNM = binding.etTNM.text.toString()
var stuSubtype = binding.etSubtype.text.toString()
var stuTreatment = binding.etTreatment.text.toString()
var stuNeochemotherapy = binding.etNeochemotherapy.text.toString()
var stuBPCR = binding.etBPCR.text.toString()
var stuTPCR = binding.etTPCR.text.toString()
var stuRe = binding.etRe.text.toString()
var stuBilateral = binding.etBilateral.text.toString()
var stuFamilyhistory = binding.etFamilyhistory.text.toString()
var stu = Student(
stuId,
stuName,
stuPhone,
stuDiagnoseyear,
stuAge,
stuTNM,
stuSubtype,
stuTreatment,
stuNeochemotherapy,
stuBPCR,
stuTPCR,
stuRe,
stuBilateral,
stuFamilyhistory
)
var flag = (studentDao.queryById(stuId) != null)
when (p0?.id) {
R.id.btn_insert -> {
if (flag) {
Toast.makeText(this, "患者已存在,无法添加", Toast.LENGTH_SHORT).show()
} else {
studentDao.insertStudent(stu)
Toast.makeText(this, "添加成功!", Toast.LENGTH_SHORT).show()
}
}
R.id.btn_delete -> {
if (flag) {
studentDao.deleteStudent(stu)
Toast.makeText(this, "删除成功!", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "该患者不存在,无法删除", Toast.LENGTH_SHORT).show()
}
}
R.id.btn_update -> {
if (flag) {
studentDao.updateStudent(stu)
Toast.makeText(this, "修改成功!", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "该患者不存在,无法修改", Toast.LENGTH_SHORT).show()
}
}
R.id.btn_query -> {
if (flag) {// 如果存在则补全信息
binding.etName.setText(studentDao.queryById(stuId)?.name)
binding.etPhone.setText(studentDao.queryById(stuId)?.phone)
binding.etDiagnoseyear.setText(studentDao.queryById(stuId)?.diagnoseyear)
binding.etAge.setText(studentDao.queryById(stuId)?.age)
binding.etTNM.setText(studentDao.queryById(stuId)?.TNM)
binding.etSubtype.setText(studentDao.queryById(stuId)?.subtype)
binding.etTreatment.setText(studentDao.queryById(stuId)?.treatment)
binding.etNeochemotherapy.setText(studentDao.queryById(stuId)?.neochemotherapy)
binding.etBPCR.setText(studentDao.queryById(stuId)?.bPCR)
binding.etTPCR.setText(studentDao.queryById(stuId)?.tPCR)
binding.etRe.setText(studentDao.queryById(stuId)?.re)
binding.etBilateral.setText(studentDao.queryById(stuId)?.bilateral)
binding.etFamilyhistory.setText(studentDao.queryById(stuId)?.familyhistory)
Toast.makeText(this, "查询到该患者信息", Toast.LENGTH_SHORT).show()
} else {// 不存在则显示所有学生信息
studentList = studentDao.queryAllStudent()
adapter = StudentAdapter(this, R.layout.item_student, studentList)
binding.lvStu.adapter = adapter
Toast.makeText(this, "查询所有患者信息", Toast.LENGTH_SHORT).show()
}
}
R.id.btn_dbtoexcel -> {
createExcel()
}
}
}
// 导出为Excel
@SuppressLint("Range")
private fun createExcel() {
try {
// 创建Excel文件stu.xls
val file = File(getExternalFilesDir(null), "stu.xls")
val workbook = Workbook.createWorkbook(file)
val sheet = workbook.createSheet("Sheet1", 0)
// 添加列名,从0开始计数
val label1 = Label(0, 0, "name")
val label2 = Label(1, 0, "phone")
val label3 = Label(2, 0, "diagnoseyear")
val label4 = Label(3, 0, "age")
val label5 = Label(4, 0, "TNM")
val label6 = Label(5, 0, "treatment")
val label7 = Label(6, 0, "neochemotherapy")
val label8 = Label(7, 0, "bPCR")
val label9 = Label(8, 0, "tPCR")
val label10 = Label(9, 0, "re")
val label11 = Label(10, 0, "bilateral")
val label12 = Label(11, 0, "familyhistory")
//从1开始计数
sheet.addCell(label1)
sheet.addCell(label2)
sheet.addCell(label3)
sheet.addCell(label4)
sheet.addCell(label5)
sheet.addCell(label6)
sheet.addCell(label7)
sheet.addCell(label8)
sheet.addCell(label9)
sheet.addCell(label10)
sheet.addCell(label11)
sheet.addCell(label12)
// 读取数据库数据stu.db,这个student是哪里来的?改成大写Student
val db = SQLiteDatabase.openOrCreateDatabase(getDatabasePath("stu.db"), null)
val cursor = db.rawQuery("select * from Student", null)
// 添加数据到Excel文件
var row = 1
while (cursor.moveToNext()) {
val name = cursor.getString(cursor.getColumnIndex("name"))
val phone = cursor.getString(cursor.getColumnIndex("phone"))
val diagnoseyear = cursor.getInt(cursor.getColumnIndex("diagnoseyear"))
val age = cursor.getString(cursor.getColumnIndex("age"))
val TNM = cursor.getString(cursor.getColumnIndex("TNM"))
val treatment = cursor.getString(cursor.getColumnIndex("treatment"))
val neochemotherapy = cursor.getString(cursor.getColumnIndex("neochemotherapy"))
val bPCR = cursor.getString(cursor.getColumnIndex("bPCR"))
val tPCR = cursor.getString(cursor.getColumnIndex("tPCR"))
val re = cursor.getString(cursor.getColumnIndex("re"))
val bilateral = cursor.getString(cursor.getColumnIndex("bilateral"))
val familyhistory = cursor.getString(cursor.getColumnIndex("familyhistory"))
//从0开始计数
val data1 = Label(0, row, name)
val data2 = Label(1, row, phone)
val data3 = Label(2, row, diagnoseyear.toString())
val data4 = Label(3, row, age)
val data5 = Label(4, row, TNM)
val data6 = Label(5, row, treatment)
val data7 = Label(6, row, neochemotherapy)
val data8 = Label(7, row, bPCR)
val data9 = Label(8, row, tPCR)
val data10 = Label(9, row, re)
val data11 = Label(10, row, bilateral)
val data12 = Label(11, row, familyhistory)
//从1开始计数
sheet.addCell(data1)
sheet.addCell(data2)
sheet.addCell(data3)
sheet.addCell(data4)
sheet.addCell(data5)
sheet.addCell(data6)
sheet.addCell(data7)
sheet.addCell(data8)
sheet.addCell(data9)
sheet.addCell(data10)
sheet.addCell(data11)
sheet.addCell(data12)
row++
}
// 关闭数据库和Excel文件
cursor.close()
db.close()
workbook.write()
workbook.close()
Toast.makeText(this, "Excel文件已导出,我的手机/Android/data/com.android.smis/files/stu.xls", Toast.LENGTH_SHORT).show()
} catch (e: Exception) {
e.printStackTrace()
}
}
}
Student.kt
package com.android.smis
class Student(val id:String, val name:String, val phone:String,val diagnoseyear:String, val age:String, val TNM:String, val subtype:String, val treatment:String, val neochemotherapy:String, val bPCR:String, val tPCR:String, val re:String, val bilateral:String, val familyhistory:String ){
}
StudentAdapter.kt
package com.android.smis
import android.app.Activity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.TextView
class StudentAdapter(activity: Activity, val resourceId: Int, data:List<Student>) :
ArrayAdapter<Student>(activity, resourceId, data){
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val view = LayoutInflater.from(context).inflate(resourceId, parent, false)
val tvId:TextView = view.findViewById(R.id.tv_id)
val tvName:TextView = view.findViewById(R.id.tv_name)
val tvPhone:TextView = view.findViewById(R.id.tv_phone)
val tvDiagnoseyear:TextView = view.findViewById(R.id.tv_diagnoseyear)
val tvAge:TextView = view.findViewById(R.id.tv_age)
val tvTNM:TextView = view.findViewById(R.id.tv_TNM)
val tvSubtype:TextView = view.findViewById(R.id.tv_subtype)
val tvTreatment:TextView = view.findViewById(R.id.tv_treatment)
val tvNeochemotherapy:TextView = view.findViewById(R.id.tv_neochemotherapy)
val tvBPCR:TextView = view.findViewById(R.id.tv_bPCR)
val tvTPCR:TextView = view.findViewById(R.id.tv_tPCR)
val tvRe:TextView = view.findViewById(R.id.tv_re)
val tvBilateral:TextView = view.findViewById(R.id.tv_bilateral)
val tvFamilyhistory:TextView = view.findViewById(R.id.tv_familyhistory)
val stu = getItem(position) // 获取当前项的学生实例
if (stu != null) {
tvId.text = stu.id
tvName.text = stu.name
tvPhone.text = stu.phone
tvDiagnoseyear.text = stu.diagnoseyear
tvAge.text = stu.age
tvTNM.text = stu.TNM
tvSubtype.text = stu.subtype
tvTreatment.text = stu.treatment
tvNeochemotherapy.text = stu.neochemotherapy
tvBPCR.text = stu.bPCR
tvTPCR.text = stu.tPCR
tvRe.text = stu.re
tvBilateral.text = stu.bilateral
tvFamilyhistory.text = stu.familyhistory
}
return view
}
}
StudentDao.kt
package com.android.smis
import android.content.ContentValues
import android.content.Context
import android.database.sqlite.SQLiteDatabase
class StudentDao(context: Context) {
val dbHelper = DBHelper(context, "stu.db", 1)
lateinit var db:SQLiteDatabase
fun openDB() {
db = dbHelper.writableDatabase
}
fun closeDB() {
if (db != null) dbHelper.close()
}
// 插入学生
fun insertStudent(stu: Student) {
val values = ContentValues().apply {
put("id", stu.id)
put("name", stu.name)
put("phone", stu.phone)
put("diagnoseyear", stu.diagnoseyear)
put("age", stu.age)
put("TNM", stu.TNM)
put("subtype", stu.subtype)
put("treatment", stu.treatment)
put("neochemotherapy", stu.neochemotherapy)
put("bPCR", stu.bPCR)
put("tPCR", stu.tPCR)
put("re", stu.re)
put("bilateral", stu.bilateral)
put("familyhistory", stu.familyhistory)
}
db.insert("Student", null, values)
}
// 删除学生
fun deleteStudent(stu: Student) {
db.delete("Student", "id = ?", arrayOf(stu.id))
}
// 更新学生
fun updateStudent(stu: Student) {
val values = ContentValues()
values.put("name", stu.name)
values.put("phone", stu.phone)
values.put("diagnoseyear", stu.diagnoseyear)
values.put("age", stu.age)
values.put("TNM", stu.TNM)
values.put("subtype", stu.subtype)
values.put("treatment", stu.treatment)
values.put("neochemotherapy", stu.neochemotherapy)
values.put("bPCR", stu.bPCR)
values.put("tPCR", stu.tPCR)
values.put("re", stu.re)
values.put("bilateral", stu.bilateral)
values.put("familyhistory", stu.familyhistory)
db.update("Student", values, "id = ?", arrayOf(stu.id))
}
// 查询所有学生
fun queryAllStudent():ArrayList<Student> {
val cursor = db.query("Student", null, null, null, null, null, null)
val stuList = ArrayList<Student>()
if (cursor.moveToFirst()) {
do {
val id = cursor.getString(cursor.getColumnIndex("id"))
val name = cursor.getString(cursor.getColumnIndex("name"))
val phone = cursor.getString(cursor.getColumnIndex("phone"))
val diagnoseyear = cursor.getString(cursor.getColumnIndex("diagnoseyear"))
val age = cursor.getString(cursor.getColumnIndex("age"))
val TNM = cursor.getString(cursor.getColumnIndex("TNM"))
val subtype = cursor.getString(cursor.getColumnIndex("subtype"))
val treatment = cursor.getString(cursor.getColumnIndex("treatment"))
val neochemotherapy = cursor.getString(cursor.getColumnIndex("neochemotherapy"))
val bPCR = cursor.getString(cursor.getColumnIndex("bPCR"))
val tPCR = cursor.getString(cursor.getColumnIndex("tPCR"))
val re = cursor.getString(cursor.getColumnIndex("re"))
val bilateral = cursor.getString(cursor.getColumnIndex("bilateral"))
val familyhistory = cursor.getString(cursor.getColumnIndex("familyhistory"))
val stu = Student(id, name,phone,diagnoseyear, age, TNM,subtype,treatment,neochemotherapy,bPCR,tPCR,re,bilateral,familyhistory)
stuList.add(stu)
} while (cursor.moveToNext())
}
cursor.close()
return stuList
}
// 查询指定学号的学生
fun queryById(stuId:String):Student? {
val cursor = db.query("Student", null, null, null, null, null, null)
if (cursor.moveToFirst()) {
do {
val id = cursor.getString(cursor.getColumnIndex("id"))
// 如果该学生存在则返回对象
if (id == stuId) {
val name = cursor.getString(cursor.getColumnIndex("name"))
val phone = cursor.getString(cursor.getColumnIndex("phone"))
val diagnoseyear = cursor.getString(cursor.getColumnIndex("diagnoseyear"))
val age = cursor.getString(cursor.getColumnIndex("age"))
val TNM = cursor.getString(cursor.getColumnIndex("TNM"))
val subtype = cursor.getString(cursor.getColumnIndex("subtype"))
val treatment = cursor.getString(cursor.getColumnIndex("treatment"))
val neochemotherapy = cursor.getString(cursor.getColumnIndex("neochemotherapy"))
val bPCR = cursor.getString(cursor.getColumnIndex("bPCR"))
val tPCR = cursor.getString(cursor.getColumnIndex("tPCR"))
val re = cursor.getString(cursor.getColumnIndex("re"))
val bilateral = cursor.getString(cursor.getColumnIndex("bilateral"))
val familyhistory = cursor.getString(cursor.getColumnIndex("familyhistory"))
val stu = Student(id, name,phone,diagnoseyear, age, TNM,subtype,treatment,neochemotherapy,bPCR,tPCR,re,bilateral,familyhistory)
cursor.close()
return stu
}
} while (cursor.moveToNext())
}
cursor.close()
return null
}
}
build.gradle文件
导入jxl包依赖 implementation 'net.sourceforge.jexcelapi:jxl:2.6.12'
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdkVersion 33
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.android.smis"
minSdkVersion 22
targetSdkVersion 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
viewBinding true
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
implementation 'net.sourceforge.jexcelapi:jxl:2.6.12'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}