Android中SQLite数据库增删改查/使用ListView显示数据库内容(有完整源码)

android作业笔记

文章目录

效果展示

编写SQLite数据库相关操作的代码,实现下图中的功能(第一排按钮布局没有调整屏幕大小适配…不过下面那一排加了 android:layout_weight=”1″)

SQLite展示

一、前言

源码获取

先上源码:https://gitee.com/meng-fanyang/SQLiteWork

Android中SQLite数据库增删改查/使用ListView显示数据库内容(有完整源码)
里边有三个分支,对应这不同的写法:
  1. master主分支是写的可以说是最完善的了
  2. nogood_branch分支把该有的功能都实现了,但是代码冗余度较大
  3. AddListView是存有刚能点击全部显示在ListView显示数据
    Android中SQLite数据库增删改查/使用ListView显示数据库内容(有完整源码)

; 实验功能描述

  1. 使用安卓自带的SQLite数据库实现数据的增删改查
  2. 点击添加数据只显示新添加的和连续点击添加的
  3. 全部删除时数据不再显示
  4. 使用ListView控件显示数据

注意事项

  1. 表的主键必须是 == _id == (_id_id_id重要的事情说三遍),具体解释下次更新(我的源码中建表第一次写错了,没有换,请读者自己建立对的表)
  2. 使用了SimpleCursorAdapter来方便数据库和listView之间的数据传递
  3. 代码中很多是注释掉的,有一部分是用来在控制台的Logcat输出的,可以在调试时查看具体情况

实现步骤

创建数据库和数据表的步骤

  1. 新建类继承SQLiteOpenHelper
  2. 实现构造方法
  3. 重写onCreate方法
  4. 重写onUpgrade方法
  5. 实例化SQLiteOpenHelper 的子类对象- MyOpenHelper;
  6. 实现点击事件

二、代码展示

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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓名:"
            />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/name"
            android:hint="请输入姓名"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="年龄:"
            />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/age"
            android:hint="请输入年龄"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="身高:"
            />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/height"
            android:hint="请输入身高"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="5dp"
            android:text="添加数据"
            android:onClick="addData"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="5dp"
            android:text="全部显示"
            android:onClick="showAll"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="5dp"
            android:text="清除显示"
            android:onClick="cleanShow"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="全部删除"
            android:onClick="deleteAll"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="ID:"
            />
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18dp"
            android:hint="输入ID"
            android:id="@+id/et_id"
            android:layout_weight="1"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="5dp"
            android:layout_weight="1"
            android:text="ID删除"
            android:onClick="IDDelete"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="5dp"
            android:layout_weight="1"
            android:text="ID查询"
            android:onClick="IDSearch"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="5dp"
            android:layout_weight="1"
            android:text="ID更新"
            android:onClick="IDUpdate"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="ID"
            android:textSize="25dp"></TextView>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="姓名"
            android:textSize="25dp"></TextView>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="年龄"
            android:textSize="25dp"></TextView>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="身高"
            android:textSize="25dp"></TextView>
    </LinearLayout>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/LV">
    </ListView>

</LinearLayout>

MyOpenHelper.java

package com.example.sqlitework;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MyOpenHelper extends SQLiteOpenHelper {

    private final static String SQL_PERSON_id="create table person_id" +
            "(_id integer primary key autoincrement,name text,age text,height text)";

    MyOpenHelper(Context context){
        super(context,"Allperson.db",null,1);
    }
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {

        sqLiteDatabase.execSQL(SQL_PERSON);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}

MainActivity.java

这一步部分太长了,就先不贴全部了,全部的在上边gitee仓库中可以查看

public class MainActivity extends AppCompatActivity {

    private ListView viewById;
    private SimpleCursorAdapter adapter;

    String from[] = new String[]{"_id", "name", "age", "height"};
    int[] to = new int[]{R.id.tv_id, R.id.tv_name, R.id.tv_age, R.id.tv_height};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MyOpenHelper myOpenHelper = new MyOpenHelper(this);

        SQLiteDatabase sqLiteDatabase = myOpenHelper.getReadableDatabase();

        sqLiteDatabase.close();

        viewById = (ListView) findViewById(R.id.LV);
    }

    public void addData(View view){
        Log.d(MainActivity.class.getSimpleName(),"=============接收到插入数据点击==============");
        count = count+1;
        nameText = (EditText) findViewById(R.id.name);
        ageText = (EditText) findViewById(R.id.age);
        heightText = (EditText) findViewById(R.id.height);

        String Name = nameText.getText().toString();
        String Age = ageText.getText().toString();
        String Height = heightText.getText().toString();

        MyOpenHelper myOpenHelper = new MyOpenHelper(this);
        SQLiteDatabase sqLiteDatabase = null;

        try {

            sqLiteDatabase = myOpenHelper.getReadableDatabase();

            sqLiteDatabase.beginTransaction();

            ContentValues contentValues = new ContentValues();
            contentValues.put("name", Name);
            contentValues.put("age", Age);
            contentValues.put("height", Height);
            sqLiteDatabase.insert("person_id",null,contentValues);
            Toast.makeText(this, "Successful insert data", Toast.LENGTH_SHORT).show();

            sqLiteDatabase.setTransactionSuccessful();
            sqLiteDatabase.endTransaction();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            String limit = String.valueOf(count);
            Cursor cursor = sqLiteDatabase.query("person_id", new String[]{"_id","name","age","height"},null,
                    null,null,null,"_id desc" , limit);

            adapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, from, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
            viewById.setAdapter(adapter);

            if(sqLiteDatabase != null){

                sqLiteDatabase.close();
            }
        }
    }

List_item.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">
    <TextView
        android:id="@+id/tv_id"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:text="list_item"
        android:gravity="center"
        android:layout_weight="1"></TextView>
    <TextView
        android:id="@+id/tv_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:text="list_item"
        android:gravity="center"
        android:layout_weight="1"></TextView>
    <TextView
        android:id="@+id/tv_age"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:text="list_item"
        android:gravity="center"
        android:layout_weight="1"></TextView>
    <TextView
        android:id="@+id/tv_height"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:text="list_item"
        android:gravity="center"
        android:layout_weight="1"></TextView>
</LinearLayout>

三、(补充)ListView实现数据列表显示

要将数据库中的数据列表显示在屏幕上,我们要使用ListView这个控件,当用户从数据库中取出数据时,要将数据绑定到显示控件上,如何绑定呢,我们需要创建适配器进行绑定,创建适配器有两种方式:
第一种是用SimpleAdapter创建(要求绑定的数据是List

参考文章:
Android开发使用SQLite数据库和Listview实现数据的存储与展示
Android创建SQLite数据库和表
android中sqlite数据库的基本使用和添加多张表
Android——ListView与数据库的结合

Original: https://blog.csdn.net/mfysss/article/details/127870757
Author: Msss-
Title: Android中SQLite数据库增删改查/使用ListView显示数据库内容(有完整源码)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/816262/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球