SQHelper
пакет com.example.sqllite
класс SQHelper (context: Context): SQLiteOpenHelper (context, DB_name, null, 1) {
переопределить fun onCreate (db: SQLiteDatabase?) {db? .exe cSQL ("создать таблицу $ TB_name (ID INTEGER PRIMARY KEY AUTOINCREMENT, S_title TEXT, S_title c TEXT) ")}
переопределить fun onUpgrade (db: SQLiteDatabase ?, oldVersion: Int, newVersion: Int) {db? .Exe cSQL («DROP TABLE, ЕСЛИ СУЩЕСТВУЕТ $ TB_name»)
}
fun ADD_DATA (title_text: String, desc_text: String) {
val DB = this.writableDatabase
val values = ContentValues()
values.put(title,title_text)
values.put(desc, desc_text)
DB.insert(TB_name,null, values )
}
fun delete_data (id: String): Int {
val DB = this.writableDatabase
val item = DB.delete(TB_name,"id = ?", arrayOf(id))
return item
}
/ * fun deleteTask (_id: Int): Boolean {val db = this.writableDatabase val _success = db.delete (TB_name, ID + "=?", arrayOf (_id.toString ())). toLong () db.close () return Integer.parseInt ("$ _ success")! = -1} * /
val data_getter: Cursor get () {val DB = this.writableDatabase var d ata = DB.rawQuery ("выберите * из" + TB_name, null)
return data
}
сопутствующий объект {var DB_name = "subjects.db" var TB_name = "Subjects" var id = "id" var title = "S_title" var des c = "S_des c"}
}
пакет com.example.sqllite
Адаптер класса (var context: Context, data: ArrayList): RecyclerView.Adapter () {
var data: List
init {this .data = data}
переопределить fun onCreateViewHolder (parent: ViewGroup, viewType: Int): ViewHolder {val layout = LayoutInflater.from (context) .inflate (R.layout.item_subject, parent, false) вернуть ViewHolder (макет)}
переопределить забаву onBindViewHolder (holder: ViewHolder, position: Int) {holder.title.text = data [position] .title holder.des c .text = data [position] .desc holder.id.text = data [position] .id holder.btndel.setOnClickListener {position}
}
override fun getItemCount (): Int {return data.size} * 106 1 *
class ViewHolder (item: View): RecyclerView.ViewHolder (item) {var title: TextView var id: TextView var des c: TextView var btndel: Button
init {
title = item.findViewById(R.id.titleTextv)
id = item.findViewById(R.id.titleTextv2)
desc = item.findViewById(R.id.descTextv)
btndel = item.findViewById(R.id.btnDelete)
}
}
}