Android studio database (content provider)

Project one:

<provider
    android:name=".MyContentProvider"
    android:authorities="com.example.xryapplication8.provider"
    android:enabled="true"
    android:exported="true"></provider>

ContentProvider:

class MyContentProvider : ContentProvider() {

    private val studentDir = 0
    private val studentItem = 1
    private val authority = "com.example.xryapplication8.provider"
    private var dbHelper : MyDatabaseHelper ?= null

    private val uriMatcher by lazy {
        val matcher = UriMatcher(UriMatcher.NO_MATCH)
        matcher.addURI(authority,"student",studentDir)
        matcher.addURI(authority,"student/#",studentItem)
        matcher
    }
    override fun delete(uri: Uri, selection: String?, selectionArgs: Array<String>?) = dbHelper?.let {

        val db = it. writableDatabase
        val deletedRows = when(uriMatcher.match(uri)){
            studentDir -> db.delete("Student",selection,selectionArgs)
            studentItem -> {
                val studentId = uri. pathSegments[1]
                db.delete("Student","id = ?", arrayOf(studentId))
            }
            else -> 0
        }
        deletedRows
    }?:0

    override fun getType(uri: Uri) = when(uriMatcher.match(uri)) {
        studentDir -> "vnd.android.cursor.dir/vnd.com.example.xryapplication8.provider.student"
        studentItem -> "vnd.android.cursor.item/vnd.com.example.xryapplication8.provider.student"
        else -> null
    }

    override fun insert(uri: Uri, values: ContentValues?) = dbHelper?.let {
        val db = it. writableDatabase
        val uriReturn = when(uriMatcher.match(uri)){
            studentDir, studentItem->{
                val newStuId = db.insert("Student",null,values)
                Uri.parse("content://$authority/student/$newStuId")
            }
            else -> null
        }
        uriReturn
    }

    override fun onCreate() = context?.let{
        dbHelper = MyDatabaseHelper(it,"Student.db",2)
        true
    }?: false

    override fun query(
        uri: Uri, projection: Array<String>?, selection: String?,
        selectionArgs: Array<String>?, sortOrder: String?
    ) = dbHelper?.let {
        val db = it. readableDatabase
        val cursor = when(uriMatcher.match(uri)){
            studentDir -> db.query("Student",projection,selection,selectionArgs,null,null,sortOrder)
            studentItem -> {
                val stuId = uri.pathSegments[1]
                db.query("Student",projection,"id= ?", arrayOf(stuId),null,null,sortOrder)
            }
            else -> null
        }
        cursor
    }

    override fun update(
        uri: Uri, values: ContentValues?, selection: String?,
        selectionArgs: Array<String>?
    ) = dbHelper?.let {
       val db = it. writableDatabase
        val updatedRows = when(uriMatcher.match(uri)){
            studentDir -> db.update("Student",values,selection,selectionArgs)
            studentItem -> {
                val studentId = uri. pathSegments[1]
                db.update("Student",values,"id=?", arrayOf(studentId))
            }
            else ->0
        }
        updatedRows
    }?:0

}

database:

class MyDatabaseHelper(val context: Context,name:String,version:Int) : SQLiteOpenHelper(context,name,null,version) {
    private val createStudent = "create table Student (" +
            "id integer primary key," +
            "name text," +
            "college text," +
            "age integer," +
            "phone integer)"

    override fun onCreate(db: SQLiteDatabase?) {
        db?.execSQL(createStudent)
    }

    override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
        db?.execSQL("drop table if exists Student")
        onCreate(db)
    }

}

Project 2:

insert:

class xryActivity1 :AppCompatActivity() ,View.OnClickListener{

    var stuId:String?=null
    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_xry1)
        val button:Button = findViewById(R.id.button)
        button. setOnClickListener (this)
    }

    override fun onClick(v: View?) {//increase
        val uri = Uri.parse("content://com.example.xryapplication8.provider/student")
        val editText1 : EditText = findViewById(R.id.editTextid)
        val editText2 : EditText = findViewById(R.id.editTextname)
        val editText3 : EditText = findViewById(R.id.editTextcollege)
        val editText4 : EditText = findViewById(R.id.editTextage)
        val editText5 : EditText = findViewById(R.id.editTextphone)
        when(v?.id){
            R.id.button -> {
                val inputText1 = editText1. text. toString()
                val inputText2 = editText2. text. toString()
                val inputText3 = editText3. text. toString()
                val inputText4 = editText4. text. toString()
                val inputText5 = editText5. text. toString()
                val values = contentValuesOf("id" to inputText1,"name" to inputText2,
                "college" to inputText3,"age" to inputText4,"phone" to inputText5)
                val newUri = contentResolver.insert(uri,values)
                stuId = newUri?.pathSegments?.get(1)

                val intent = Intent(this,MainActivity::class.java)
                startActivityForResult(intent,1)
            }


        }
    }
}

Inquire:

class xryActivity2 :AppCompatActivity(),AdapterView.OnItemClickListener {
    private val list = ArrayList<student>()

    @SuppressLint("Range")
    override fun onCreate(savedInstanceState:Bundle?){
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recyclerview)
        val listview:ListView = findViewById(R.id.listView)
        val adapter = StuAdapter(R.layout.item,list,this)
        listview.adapter = adapter


        listview.setOnItemClickListener(this)

        val uri = Uri.parse("content://com.example.xryapplication8.provider/student")
        contentResolver.query(uri,null,null,null,null)?.apply {
            while(moveToNext()){//find
                val name = getString(getColumnIndex("name"))
                val id = getInt(getColumnIndex("id"))
                val college = getString(getColumnIndex("college"))
                val stu = student(name, id, college)
                list. add(stu)
            }
            close()
        }
    }
    
    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
        val stus = list[position]

        val intent = Intent(this,xryActivity3::class.java)
        intent. putExtra("stu_id", stus. id. toString())
        startActivity(intent)
    }
    
}

Modifications and deletions:

class xryActivity3 :AppCompatActivity(),View.OnClickListener{

    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_xry3)
        val button1 :Button = findViewById(R.id.button01)
        button1.setOnClickListener(this)
        val button2: Button = findViewById(R.id.button02)
        button2.setOnClickListener(this)

        val textView:TextView = findViewById(R.id.textViewid)
        textView.text = intent.getStringExtra("stu_id").toString()
    }


    override fun onClick(v: View?) {
        val studentId = intent. getStringExtra("stu_id")
        val textView:TextView =findViewById(R.id.textViewid)
        textView.text=studentId
        val editText1: EditText = findViewById(R.id.editTextname)
        val editText2: EditText = findViewById(R.id.editTextcollege)
        val editText3: EditText = findViewById(R.id.editTextage)
        val editText4: EditText = findViewById(R.id.editTextphone)
       when(v?.id){

           R.id.button01 ->{//Modify
              studentId?.let {
                  val inputText1 = editText1. text. toString()
                  val inputText2 = editText2. text. toString()
                  val inputText3 = editText3. text. toString()
                  val inputText4 = editText4. text. toString()
                  val uri = Uri.parse("content://com.example.xryapplication8.provider/student/$it")
                  val values = contentValuesOf("name" to inputText1,"college" to inputText2,
                  "age" to inputText3,"phone" to inputText4)
                  contentResolver. update(uri, values, null, null)
                  val intent = Intent(this,xryActivity2::class.java)
                  startActivityForResult(intent,1)
              }
           }
           R.id.button02 ->{//delete
               studentId?.let {
                   val uri = Uri.parse("content://com.example.xryapplication8.provider/student/$it")
                   contentResolver.delete(uri,null,null)
                   val intent = Intent(this,xryActivity2::class.java)
                   startActivityForResult(intent,1)
               }
           }
       }
    }
}

student:

class student (var name:String,var id:Int,var college:String)

Adapter:

class StuAdapter(var resourceId:Int,var stulist:ArrayList<student>,var context: Context):
    BaseAdapter() {
    @SuppressLint("MissingInflatedId")
    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        var v: View = LayoutInflater.from(context).inflate(R.layout.item, null)
        val model: student = stulist[position]
        val name: TextView = v.findViewById(R.id.studentname)
        val id: TextView = v.findViewById(R.id.studentid)
        val college: TextView = v.findViewById(R.id.studentcollege)
        name.text = model.name
        id.text = model.id.toString()
        college.text = model.college
        return v


    }

    override fun getCount(): Int {
        return stulist.size
    }

    override fun getItem(position: Int): student? {
        return stulist[position]
    }

    override fun getItemId(position: Int): Long {
        return 0
    }
}

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge MySQL entry skill tree Database composition Table 55662 people are studying systematically