当前位置: 首页 > news >正文

如何加强网站建设和信息宣传宁波网络推广

如何加强网站建设和信息宣传,宁波网络推广,广告公司起名字大全,做2手车网站需要多少钱文章目录一、实验内容二、实验步骤1、页面布局2、数据库3、登录活动4、增删改查三、运行演示四、实验总结五、源码下载一、实验内容 根据Android数据存储的内容,综合应用SharedPreferences和SQLite数据库实现一个用户信息管理系统,强化对SharedPreferen…

文章目录

  • 一、实验内容
  • 二、实验步骤
    • 1、页面布局
    • 2、数据库
    • 3、登录活动
    • 4、增删改查
  • 三、运行演示
  • 四、实验总结
  • 五、源码下载

一、实验内容

根据Android数据存储的内容,综合应用SharedPreferences和SQLite数据库实现一个用户信息管理系统,强化对SharedPreferences的理解的使用,熟练掌握SQLite的操作。要求:

  1. 巩固Android应用开发工具(Eclipse或者AndroidStudio)的常规用法;
  2. 巩固Activity、UI控件的常规用法;
  3. 掌握SharedPpreferences数据存储的使用;
  4. 掌握SQLite数据库及SQLiteOpenHelper的使用。

二、实验步骤

1、页面布局

本次布局提倡从简原则,按照往常习惯,我肯定是创建多个Activity,然后每个Activity设置下页面,分别从主页面跳转到各个页面。既然是实验,那就从简,实现核心的思想就可以了,底层逻辑实现出来,表面内容那不是花时间设计下就行了。言归正传,主页面布局如下,没有任何亮点可言,比较常规,只给Button和TextView都设置了background。

在这里插入图片描述
完整的layout代码如下:

<?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"><EditTextandroid:id="@+id/et_id"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="请输入学号"android:textSize="20sp"android:textColor="@color/black"android:padding="10dp"android:layout_margin="20dp"android:inputType="text"android:background="@drawable/et_selector" /><EditTextandroid:id="@+id/et_name"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="请输入姓名"android:textSize="20sp"android:textColor="@color/black"android:padding="10dp"android:layout_margin="20dp"android:inputType="text"android:background="@drawable/et_selector" /><EditTextandroid:id="@+id/et_age"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="请输入年龄"android:textSize="20sp"android:textColor="@color/black"android:padding="10dp"android:layout_margin="20dp"android:inputType="text"android:background="@drawable/et_selector" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonandroid:id="@+id/btn_insert"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"android:text="添加"android:background="@drawable/btn_selector"android:textSize="20sp"android:layout_margin="10dp"android:textColor="@color/black"/><Buttonandroid:id="@+id/btn_delete"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"android:text="删除"android:background="@drawable/btn_selector"android:textSize="20sp"android:layout_margin="10dp"android:textColor="@color/black"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_marginBottom="20dp"><Buttonandroid:id="@+id/btn_update"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"android:text="修改"android:background="@drawable/btn_selector"android:textSize="20sp"android:layout_margin="10dp"android:textColor="@color/black"/><Buttonandroid:id="@+id/btn_query"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"android:text="查询"android:background="@drawable/btn_selector"android:textSize="20sp"android:layout_margin="10dp"android:textColor="@color/black"/></LinearLayout><Viewandroid:layout_width="match_parent"android:layout_height="2dp"android:background="@color/black"/><LinearLayoutandroid:orientation="horizontal"android:layout_width="match_parent"android:layout_height="wrap_content"><TextViewandroid:layout_width="0dp"android:layout_weight="1"android:text="学号"android:textSize="20sp"android:gravity="center"android:layout_height="wrap_content"/><TextViewandroid:layout_width="0dp"android:layout_weight="1"android:text="姓名"android:textSize="20sp"android:gravity="center"android:layout_height="wrap_content"/><TextViewandroid:layout_width="0dp"android:layout_weight="1"android:text="年龄"android:textSize="20sp"android:gravity="center"android:layout_height="wrap_content"/></LinearLayout><Viewandroid:layout_width="match_parent"android:layout_height="2dp"android:background="@color/black"/><ListViewandroid:id="@+id/lv_stu"android:layout_width="match_parent"android:layout_height="match_parent"/>
</LinearLayout>

2、数据库

考查对SQLite的熟练程度,其实就是Android提供了一个数据库帮助类,帮我们进行数据库的各种操作。我们要做的就是建库建表,写个增删改查的方法,然后剩下的事情交给系统。这里是学生表的建表语句,一切属性都能用text表示。

    private val CREATE_STUDENT = "create table Student (" +"id text primary key," +"name text," +"age text)"

再看学生类,简直封装的太好了,Kotlin的魅力所在,换做Java又是属性、构造函数、get和set方法。

class Student(val id:String, val name:String, val age:String) {}

下面看数据库的增删改查操作,所有的操作都是针对数据库的Student表来的,增加、删除和修改都很简单,使用ContentValues添加键值对。查询是最关键的,使用cursor游标一行行遍历表数据,各种约束条件可以自己加,正常全查就完事了。

 val dbHelper = DBHelper(context, "stu.db", 1)lateinit var db:SQLiteDatabasefun 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("age", stu.age)}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("age", stu.age)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 age = cursor.getString(cursor.getColumnIndex("age"))val stu = Student(id, name, age)stuList.add(stu)} while (cursor.moveToNext())}cursor.close()return stuList}

3、登录活动

登录活动用的是sharedPreferences,它使用方法非常easy,首先初始化一个sharedPreferences对象,文件名和访问类型自定义。读数据就是调用getString获取键值对,设定个默认值。写数据就是调用sharedPreferences.edit()赋值给editor对象,然后putString读取键值对。还记录了下app的使用次数。

	override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)binding = ActivityLoginBinding.inflate(layoutInflater)setContentView(binding.root)sharedPreferences = getSharedPreferences("data", Context.MODE_PRIVATE)var editor = sharedPreferences.edit()// 得到之前的使用次数,然后每次打开app都加1var count = sharedPreferences.getString("count", "0");binding.tvCount.text = (count!!.toInt() + 1).toString()// 保存键值对到sharedpreferences中editor.putString("count", (count!!.toInt() + 1).toString())editor.apply()binding.btnLogin.setOnClickListener{editor.putString("account", binding.etAccount.toString().trim())editor.putString("password", binding.etPassword.toString().trim())editor.apply()Toast.makeText(this, "登录成功!", Toast.LENGTH_SHORT).show()val intent = Intent(this, MainActivity::class.java)startActivity(intent)finish()}}

4、增删改查

其实对数据表的增删改查逻辑在StudentDao中已经封装好了,我们在Activity里面也只是调用方法实现界面和数据库的交互罢了。具体的操作逻辑如下:

输入学号、姓名和年龄后点击添加可以添加学生;输入学号点击查询可以查询学生信息,然后点击删除会删除信息,点击修改会修改输入框中的学生信息,最后如果输入的学号不存在而且你点查询了,会显示所有学生的信息,如果存在只会显示该学生的信息。

    override fun onClick(p0: View?) {var stuId = binding.etId.text.toString()var stuName = binding.etName.text.toString()var stuAge = binding.etAge.text.toString()var stu = Student(stuId, stuName, stuAge)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.etAge.setText(studentDao.queryById(stuId)?.age)binding.etName.setText(studentDao.queryById(stuId)?.name)Toast.makeText(this, "查询到该学生信息", Toast.LENGTH_SHORT).show()} else {// 不存在则显示所有学生信息studentList = studentDao.queryAllStudent()adapter = StudentAdapter(this, R.layout.item_student, studentList)binding.lvStu.adapter = adapterToast.makeText(this, "查询所有学生信息", Toast.LENGTH_SHORT).show()}}}}

三、运行演示

1、首先进入登录界面,输入账号和密码然后点击登录即可,右上角是使用次数。

在这里插入图片描述

2、进入主界面,我们输入学号、姓名和年龄进行添加学生。

在这里插入图片描述
3、点击添加按钮,添加成功。再依次添加几个学生。

在这里插入图片描述

4、点击查询,此时学号是不存在的,所以就查询了所有学生的信息。

在这里插入图片描述
5、我们输入学号4,然后点击查询,可以看到查询到信息并自动补全了。

在这里插入图片描述
6、修改姓名和年龄,然后再点修改,再点击查询,发现已经修改好了信息。

在这里插入图片描述
在这里插入图片描述

7、我们再查询小益的信息,然后删除小益的信息。
在这里插入图片描述
在这里插入图片描述

四、实验总结

其实学生系统涉及到数据库的操作完全和前面的其他系统相似,真正做起来还是比较繁琐的。哪里有什么容易代码,都是在一个个bug解决中完成的。理论引导实战,光理论只会纸上谈兵,光实践缺少方法论,基础打牢了才能进阶,不然上限不会高,基础决定了你的上限。

五、源码下载

源代码已上传CSDN,点击下载
源代码已上传GitHub,点击下载
http://www.shuangfujiaoyu.com/news/19155.html

相关文章:

  • 沈阳企业网站开发定制如何网络媒体推广
  • 网站seo推广多少钱国际网站平台有哪些
  • 室内装修装饰设计培训班广州seo网站推广
  • 嘉兴市住房和城乡建设局网站磁力搜索引擎torrentkitty
  • 网站推广连接怎么做的飞猪关键词排名优化
  • 海南政府网站集约化建设网站推广策划思路
  • 诸城网站做的好的电脑培训零基础培训班
  • 如何用.net做网站最新疫情最新情况
  • windows wordpress mi天津做优化好的公司
  • 网站的建站程序太原网站推广公司
  • 个人空间网站模板百度惠生活怎么优化排名
  • 设计师做帆布包网站360优化大师历史版本
  • 政元软件做网站一个新手怎么去运营淘宝店铺
  • 什么网站可以用手机做兼职赚钱网站seo专员
  • 做网站之前需要准备什么买卖交易网
  • 微信推广网站怎么做我想做app推广代理
  • 上海哪家公司提供专业的网站建设站长资源平台
  • 企业网站视频栏目建设方案搜索引擎下载安装
  • 0基础做网站工具网红推广接单平台
  • 自己做文学网站赚钱吗手机百度搜索
  • 书画网站建设方案策划百度seo排名优化公司推荐
  • 学校风采网站建设需求苏州百度推广排名优化
  • 济南品牌营销型网站建设北京seo排名外包
  • win7 iis 网站网站信息
  • 广州市医院网站建设手机关键词点击排名软件
  • 什么网站可以做公务员考试题什么是关键词搜索
  • 如何和其他网站做友情链接广州疫情防控措施
  • wordpress的Portfolio单页网站seo优化
  • 自己做图片网站seo赚钱方法大揭秘
  • 石景山区住房建设委员会网站公司的seo是什么意思