Sqlite数据库增删改查——简易学生管理系统demo

这篇博客展示了如何使用Android Studio创建一个简单的SQLite数据库,实现学生信息的增删改查功能。通过AddActivity、MainActivity、SearchActivity等类进行交互,使用ListView展示数据,并通过Student、StudentAdapter和StudentDao等类进行数据处理。文章提供了参考资源和源码下载链接。
摘要由CSDN通过智能技术生成

Android studio写的一个简单的数据库增删改查demo。便于初步理解数据库操作。

1.实现效果

2.项目目录

3.具体Activity与Layout实现

1> studentdemo包,主要是Activity

AddActivity.java

package com.example.rex.studentdemo;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.rex.student.Student;
import com.example.rex.student.StudentDao;

public class AddActivity extends AppCompatActivity {

    private Button resetButton, okButton;
    private EditText nameEditText, ageEditText;

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

        initView();
        onButtonClick();
    }

    private void initView()
    {
        resetButton = (Button) findViewById(R.id.add_reset_btn);
        okButton = (Button) findViewById(R.id.add_ok_btn);

        nameEditText = (EditText) findViewById(R.id.edit_text_name);
        ageEditText = (EditText) findViewById(R.id.edit_text_age);
    }

    private void onButtonClick()
    {
        resetButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //有可能出问题,没有出问题...
                nameEditText.setText("");
                ageEditText.setText("");
            }
        });

        okButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(nameEditText.getText() != null && ageEditText.getText() != null)
                {
                    String name = nameEditText.getText() + "";
                    int age = Integer.parseInt(ageEditText.getText() + "");

                    Student student = new Student(name, age);
                    StudentDao.insertData(AddActivity.this, student);
                }
                Intent i = new Intent(AddActivity.this, MainActivity.class);
                startActivity(i);
            }
        });
    }
}

对应布局文件activity_add.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓名:"/>
        <EditText
            android:id="@+id/edit_text_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="年龄:"/>
        <EditText
            android:id="@+id/edit_text_age"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLength="2"
            android:inputType="number"/>
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/add_reset_btn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="重置"/>
        <Button
            android:id="@+id/add_ok_btn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="保存"/>
    </LinearLayout>

</LinearLayout>

MainActivity.java

package com.example.rex.studentdemo;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.example.rex.student.Student;
import com.example.rex.student.StudentAdapter;
import com.example.rex.student.StudentDao;
import com.example.rex.utils.DbManager;
import com.example.rex.utils.MySqliteHelper;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private MySqliteHelper helper;
    private Button searchButton;
    private Button addButton;
    private ListView listView;
    private List<Student> list = new ArrayList<>();

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

        helper = DbManager.getHelperInstance(this);
        helper.getWritableDatabase();
        initView();
        onButtonClick();
        showListView();
        onItemClick();
    }

    //初始化按钮、ListView 控件
    private void initView()
    {
        searchButton = (Button) findViewById(R.id.search_btn);
        addButton = (Button) findViewById(R.id.add_btn);
        listView = (ListView) findViewById(R.id.list_view);
    }

    /**
     * 将ListView对象显示出来
     */
    private void showListView()
    {
        list = StudentDao.selectAll(MainActivity.this);
        StudentAdapter adapter = new StudentAdapter(MainActivity.this, R.layout.student_item, list);
        listView.setAdapter(adapter);
    }

    private void onItemClick(
  • 5
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值