Какой код я должен написать, чтобы удалить все карты в Kotlin? (Android Студия, Kotlin, SQlite) - PullRequest
0 голосов
/ 02 мая 2020
  • MainActivity
package com.example.sqllite

class MainActivity : AppCompatActivity() {

lateinit var lists:ArrayList<Subject>
lateinit var DB:SQHelper
lateinit var data:Cursor


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

 /*   val deletebtn = findViewById<Button>(R.id.btnDelete)

    deletebtn.setOnClickListener {
        deletedata
    } */

    val go = findViewById<Button>(R.id.btnGoSub)

    go.setOnClickListener {
        startActivity(Intent(this@MainActivity, AddSubject::class.java))
    }


    lists = ArrayList<Subject>()
    DB = SQHelper(applicationContext)
    data = DB.data_getter

    val adapter = Adapter(applicationContext, lists)
    val recyclerView = findViewById<RecyclerView>(R.id.list)

    ShowData()

    list.layoutManager = GridLayoutManager(applicationContext,2)
    list.adapter = adapter
 }


 fun ShowData() {

        if (data.count == 0){
            Toast.makeText(applicationContext,"There is no items",Toast.LENGTH_SHORT).show()
        }
        while (data.moveToNext())

            lists.add(Subject(data.getString(0),
                data.getString(1),
                data.getString(2)

            ))

    }

override fun onStart() {
    super.onStart()
    ShowData()
}

}
  • 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)
    }
    

    }

    }

  • AddSubject

    пакет com.example.sqllite

    class AddSubject: AppCompatActivity () {

    переопределить fun onCreate (saveInstanceState: Bundle?) {super.onCreate (saveInstanceState) setContentView (R.layout.activity_add_subject)

    val DB = SQHelper(applicationContext)
    
    val title_input = findViewById<EditText>(R.id.editTitle)
    val desc_input = findViewById<EditText>(R.id.editDesc)
    val addbtn = findViewById<Button>(R.id.btnAdd)
    
    addbtn.setOnClickListener {
    
        val title_text = title_input.text.toString().trim()
        val desc_text = desc_input.text.toString().trim()
    
        DB.ADD_DATA(title_text,desc_text)
        Toast.makeText(this@AddSubject,"The Subject has been added",Toast.LENGTH_SHORT).show()
        startActivity(Intent(this@AddSubject,MainActivity::class.java))
    
    }
    

    }}

  • activity_main. xml

  • item_subject. xml

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp"
        android:orientation="vertical"
        android:padding="15dp">
    
        <TextView
            android:id="@+id/titleTextv2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/_1"
            android:textColor="#FFFFFF"
            android:textSize="18sp" />
    
        <TextView
            android:id="@+id/titleTextv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center|center_horizontal|center_vertical"
            android:gravity="center|center_horizontal|center_vertical"
            android:text="@string/android"
            android:textColor="#FFFFFF"
            android:textSize="18sp" />
    
        <TextView
            android:id="@+id/descTextv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center|center_horizontal|center_vertical"
            android:gravity="center|center_horizontal|center_vertical"
            android:text="@string/learn_android"
            android:textColor="#FFFFFF"
            android:textSize="18sp" />
    
        <Button
            android:id="@+id/btnDelete"
            android:layout_width="100dp"
            android:layout_height="45dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="15dp"
            android:background="@drawable/buttonstyle"
            android:gravity="center"
            android:text="@string/delete"
            android:textAllCaps="false"
            android:textColor="#FFFFFF" />
    
    </LinearLayout>
    

  • activity_add_subject

    <EditText
        android:id="@+id/editTitle"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="100dp"
        android:background="@android:color/background_dark"
        android:ems="10"
        android:hint="Enter your title"
        android:inputType="textPersonName"
        android:textColor="#FFFFFF"
        android:textColorHint="#FFFFFF"
        android:textSize="24sp" />
    
    <EditText
        android:id="@+id/editDesc"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:background="@android:color/background_dark"
        android:ems="10"
        android:hint="Enter your description"
        android:inputType="textPersonName"
        android:textColor="#FFFFFF"
        android:textColorHint="#FFFFFF"
        android:textSize="24sp" />
    

...