如何在Kotlin中删除项目后更新android中的recycleView?

ffdz8vbo  于 2023-01-07  发布在  Android
关注(0)|答案(5)|浏览(154)

我已经创建了一个回收视图,并在其中使用卡片视图的项目。我有一个删除按钮内的卡片视图每当我点击该按钮我的项目从SQLite数据库中删除。但要反映在UI上,应用程序需要重新启动。我如何通知adpater该项目已删除?
activity_main.xml

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

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:layout_width="57dp"
        android:layout_height="64dp"
        android:layout_marginEnd="40dp"
        android:layout_marginBottom="40dp"
        android:clickable="true"
        android:onClick="addNewCredentials"
        app:backgroundTint="#270867"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@android:drawable/ic_menu_add" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="1dp"
        android:layout_marginTop="1dp"
        android:layout_marginEnd="1dp"
        android:layout_marginBottom="1dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" >

    </androidx.recyclerview.widget.RecyclerView>

</androidx.constraintlayout.widget.ConstraintLayout>

list_item_layout.xml

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

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="5dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/urlView"
                android:layout_width="300dp"
                android:layout_height="30dp"
                android:layout_marginTop="16dp"
                android:layout_marginBottom="16dp"
                android:text="url"
                android:textAppearance="@style/TextAppearance.AppCompat.Large"
                android:textSize="24sp"
                android:textStyle="bold"
                app:layout_constraintBottom_toTopOf="@+id/userNameView"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.1"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="1.0" />

            <TextView
                android:id="@+id/userNameView"
                android:layout_width="300dp"
                android:layout_height="25dp"
                android:layout_marginBottom="16dp"
                android:text="userName"
                app:layout_constraintBottom_toTopOf="@+id/passwordView"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.1"
                app:layout_constraintStart_toStartOf="parent" />

            <TextView
                android:id="@+id/passwordView"
                android:layout_width="300dp"
                android:layout_height="25dp"
                android:layout_marginBottom="16dp"
                android:text="password"
                app:layout_constraintBottom_toTopOf="@+id/noteView"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.1"
                app:layout_constraintStart_toStartOf="parent" />

            <TextView
                android:id="@+id/noteView"
                android:layout_width="300dp"
                android:layout_height="30dp"
                android:layout_marginBottom="16dp"
                android:text="note"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.1"
                app:layout_constraintStart_toStartOf="parent" />

            <Button
                android:id="@+id/delButton"
                android:layout_width="78dp"
                android:layout_height="40dp"
                android:layout_marginEnd="8dp"
                android:layout_marginBottom="36dp"
                android:background="#E6360F"
                android:text="@string/delete_credential_button"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent" />

        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.cardview.widget.CardView>
</LinearLayout>

MainActivity.kt

package com.example.passwordmanager

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
        recyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL,false)
        val db = DataBaseHandler(this)
        val detailsData = db.readCredentials()
        val adapter = CredentialAdapter(detailsData,this,{credentialsModel: CredentialsModel->deleteClick(credentialsModel)})
        recyclerView.adapter = adapter
    }

    fun deleteClick(credential: CredentialsModel){
        val db = DataBaseHandler(this)
        if(db.deleteData(credential.id)){
            //adapter.notifyItemRemoved(position)
            Toast.makeText(applicationContext,"Deleted", Toast.LENGTH_SHORT).show()
        }
    }

    fun addNewCredentials(view : View){
        print("hello world")
        val intent = Intent(this, AddDetailActivity::class.java)
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        startActivity(intent)
    }
}

CredentialAdapter.kt

package com.example.passwordmanager

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.list_item_layout.view.*

class CredentialAdapter(
    private val items: List<CredentialsModel>,
    ctx: Context, val clickListener: (CredentialsModel) -> Unit
): RecyclerView.Adapter<CredentialAdapter.ViewHolder>() {
    var context = ctx
    class ViewHolder(itemView: View):RecyclerView.ViewHolder(itemView){
        fun bind(credential: CredentialsModel,clickListener: (CredentialsModel) -> Unit){
            itemView.urlView.text = credential.url
            itemView.userNameView.text = credential.userName
            itemView.passwordView.text = credential.password
            itemView.noteView.text = credential.note
            itemView.delButton.setOnClickListener{clickListener(credential)}
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.list_item_layout,parent,false))
    }

    override fun getItemCount(): Int {
        return items.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val credential:CredentialsModel = items[position]
        holder.bind(credential,clickListener)
    }
}
31moq8wy

31moq8wy1#

添加删除onBindViewHolder中的setOnClickListener。

override fun onBindViewHolder(holder: ViewHolder, position: Int) {

    holder.remove.setOnClickListener {

        val db = DataBaseHandler(this)
        if(db.deleteData(credential.id)){
            notifyItemRemoved(holder.getAdapterPosition())
        }

    }
}
afdcj2ne

afdcj2ne2#

处理这类情况的最佳方法是使用LiveData。LiveData基本上是一个可观察的类,它只在发生更改时读取数据。您可以在适配器中创建一个set函数,如下所示:

internal fun setData(data: List<Data>) {
    this.data= dataList //this datalist is a list defined in your adapter 
    notifyDataSetChanged()
}

现在,在你的主Activity/片段中,在onCreate函数之外创建一个LiveData列表,如下所示:

private lateinit var allData:LiveData<List<Data>>

现在,在onCreate函数中,use可以观察livedata并为recyclerview设置数据,如下所示:

allData.observe(this, Observer { data->
        data?.let { adapter.setData(it) }
    })
jv2fixgn

jv2fixgn3#

您正在从数据库中删除项目,但未从recyclerview适配器内的列表中删除。

class CredentialAdapter(
private val items: ArrayList<CredentialsModel>, // Change list to arraylist
ctx: Context, val clickListener: (CredentialsModel, Int) -> Unit
): RecyclerView.Adapter<CredentialAdapter.ViewHolder>() {

    ...
    ...

    fun remove(position: Int) {
        // Remove and notify the adapter to reload
        items.removeAt(position)
        notifyItemRemoved(position)
    }

    class ViewHolder(itemView: View):RecyclerView.ViewHolder(itemView) {
        fun bind(credential: CredentialsModel,clickListener: (CredentialsModel, Int) -> Unit){
            ...
            ...
            // Pass adapter item position so that we can update the list after delete
            itemView.delButton.setOnClickListener{clickListener(credential, adapterPosition)
        }
    } 

    ...
    ...
}

内部主要活动.kt

fun deleteClick(credential: CredentialsModel, position: Int) {
    val db = DataBaseHandler(this)
    if(db.deleteData(credential.id)){
        adapter.remove(position)
        Toast.makeText(applicationContext,"Deleted", Toast.LENGTH_SHORT).show()
    }
}
oipij1gg

oipij1gg4#

使用ListAdpater

class AdapterMain(var onClickListener: (Int) -> Unit) :
ListAdapter<Note, AdapterMain.NoteViewHolder>(DIFFCALBACK) {

companion object DIFFCALBACK : DiffUtil.ItemCallback<Note>() {
    override fun areItemsTheSame(oldItem: Note, newItem: Note): Boolean {
        return oldItem.id == newItem.id
    }

    override fun areContentsTheSame(oldItem: Note, newItem: Note): Boolean {
        return oldItem.title == newItem.title &&
                oldItem.description == newItem.description &&
                oldItem.priority == newItem.priority
    }
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NoteViewHolder {
    val view = LayoutInflater.from(parent.context).inflate(R.layout.note_item, parent, false)
    return NoteViewHolder(view)
}

override fun onBindViewHolder(holder: NoteViewHolder, position: Int) {
    holder.txtTitle.text = getItem(position).title
    holder.txtDesc.text = getItem(position).description
    holder.txtPriority.text = getItem(position).priority.toString()
}

inner class NoteViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    var txtTitle: TextView = itemView.txt_title
    var txtDesc: TextView = itemView.txt_desc
    var txtPriority: TextView = itemView.txt_priority

    init {
        itemView.setOnClickListener { onClickListener(adapterPosition) }
    }
}

fun getNoteAt(position: Int): Note {
    return getItem(position)
}

}
you can see complete code of simple NoteApp with kotlin , recyclerView , MVVM and..

u5i3ibmn

u5i3ibmn5#

Coba类:AppCompatActivity(){私有延迟初始化变量循环视图:循环查看器私有延迟初始化变量数据:数组列表覆盖创建时的功能(保存的示例状态:捆绑包?){超级创建(保存的示例状态)设置内容视图(R.布局.活动_coba)回收视图=查找视图按ID(R.ID.rcycoba)数据=数组列表()

val dtnama = arrayOf(
        "Danial Sanganus",
        "Bijonia Skolin",
        "Alianes Pertoli",
        "Sivanian Pertici",
        "Olehsan alausi"
    )

    for (i in dtnama.indices){
        datalis.add(
            dataCoba(
                dtnama[i]
            )
        )
        populateData()

    }

}

private fun populateData(){
    val linearManager = LinearLayoutManager(this)
    linearManager.reverseLayout=true
    linearManager.stackFromEnd=true
    recycleView.layoutManager=linearManager
    val adp =adpCoba(this,datalis)
    recycleView.adapter=adp
}

}

相关问题