android实现数据库添加操作

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    >

    <TextView
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="学生管理系统"
        app:layout_constraintBottom_toTopOf="@+id/lv"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />


    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintVertical_weight="1"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/add"
        />
</androidx.constraintlayout.widget.ConstraintLayout>

dialog.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"
    android:orientation="vertical">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/dept"
        android:hint="院系"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/grade"
        android:hint="院系"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/name"
        android:hint="院系"/>
</LinearLayout>

list_view_item

<?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"
    android:orientation="horizontal">

    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:id="@+id/dept" />

    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:id="@+id/grade" />

    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:id="@+id/name" />
</LinearLayout>

MainActivity.java

package com.liuli.sujvku;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    List<Student> students;

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


        ListView lv = findViewById(R.id.lv);

        StudentDao StudentDao = new StudentDao(this);
        students = StudentDao.findall();
        StudentAdapter studentAdapter = new StudentAdapter();

        lv.setAdapter(studentAdapter);
        TextView add = findViewById(R.id.add);

        add.setOnClickListener(view ->{
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("添加学生信息");
            View v =View.inflate(this,R.layout.dialog,null);

            builder.setView(v);
            EditText dept = v.findViewById(R.id.dept);
            EditText grade = v.findViewById(R.id.grade);
            EditText name = v.findViewById(R.id.name);
            builder.setPositiveButton("保存",(dialogInterface,i)->{
                Student student = new Student();
                student.dept = dept.getText().toString();
                student.grade = grade.getText().toString();
                student.name = name.getText().toString();

                StudentDao.addStudent(student);
                students.add(student);
                studentAdapter.notifyDataSetChanged();

            });

            builder.show();
        });

    }

    class  StudentAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return students.size();
        }

        @Override
        public Student getItem(int i) {
            return students.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup){
            if(view == null){
                view = View.inflate(MainActivity.this,R.layout.list_view_item,null);
            }
            Student student = students.get(i);
            TextView dept = view.findViewById(R.id.dept);
            TextView grade = view.findViewById(R.id.grade);
            TextView name = view.findViewById(R.id.name);

            dept.setText(student.dept);
            dept.setText(student.grade);
            dept.setText(student.name);
            return view;
        }
    }


}

 

StudentDBHelper.java

package com.liuli.sujvku;

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

import androidx.annotation.Nullable;

public class StudentDBHelper extends SQLiteOpenHelper {
    public StudentDBHelper(@Nullable Context context) {
        super(context,"stu.db",null,1);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {

        String sql = "create table student (id Integer primary key autoincrement," +
                "dept varchar(20)," +
                "grade varchar(20)," +
                "name varchar(20))";

        sqLiteDatabase.execSQL(sql);

    }

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

    }
}

StudentDao

package com.liuli.sujvku;

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

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

public class StudentDao {

    SQLiteDatabase db = null;

    public StudentDao(Context ctx){
        StudentDBHelper dBHelper =new StudentDBHelper(ctx);
        db = dBHelper.getReadableDatabase();

    }


    public  void addStudent(Student student){

        String sql = "insert into student(dept,grade,name) values ('"+student.dept+"','"+student.grade+"','"+student.name+"')";
        db.execSQL(sql);
    }


    public List<Student> findall(){
        List<Student> stus = new ArrayList<>();
        String sql = "select * from student";
        Cursor cursor = db.rawQuery(sql,null);
        while(cursor.moveToNext()){
            Student student = new Student();
            student.dept = cursor.getString(0);
            student.grade = cursor.getString(1);
            student.name = cursor.getString(2);
            stus.add(student);
        }
        return stus;
    }
}

Student

package com.liuli.sujvku;

public class Student {
    public String dept;
    public String grade;
    public String name;


}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值