在删除kotlin中的项目后,如何在android中更新recycleView?
我已经创建了一个回收视图,并在其中使用项目的卡片视图。我在卡片视图中有一个删除按钮,每当我点击该按钮时,我的项目就会从SQLite数据库中删除。但要在UI上反映出来,应用程序需要重新启动。如何通知adpater项目已删除 activity_main.xml在删除kotlin中的项目后,如何在android中更新recycleView?,android,kotlin,android-recyclerview,Android,Kotlin,Android Recyclerview,我已经创建了一个回收视图,并在其中使用项目的卡片视图。我在卡片视图中有一个删除按钮,每当我点击该按钮时,我的项目就会从SQLite数据库中删除。但要在UI上反映出来,应用程序需要重新启动。如何通知adpater项目已删除 activity_main.xml <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://
<?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)
}
}
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()
}
}
package com.example.passwordmanager
导入android.content.Intent
导入androidx.appcompat.app.appcompat活动
导入android.os.Bundle
导入android.view.view
导入android.widget.Toast
导入androidx.recyclerview.widget.LinearLayoutManager
导入androidx.recyclerview.widget.recyclerview
类MainActivity:AppCompatActivity(){
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val recyclerView=findviewbyd(R.id.recyclerView)
recyclerView.layoutManager=LinearLayoutManager(此,recyclerView.VERTICAL,false)
val db=DataBaseHandler(此)
val detailsData=db.readCredentials()
val adapter=CredentialAdapter(详细数据,此,{credentialsModel:credentialsModel->deleteClick(credentialsModel)})
recyclerView.adapter=适配器
}
单击(凭证:CredentialsModel){
val db=DataBaseHandler(此)
if(db.deleteData(credential.id)){
//适配器。已移除(位置)
Toast.makeText(applicationContext,“已删除”,Toast.LENGTH\u SHORT.show())
}
}
fun addNewCredentials(视图:视图){
打印(“你好世界”)
val intent=intent(这是AddDetailActivity::class.java)
intent.flags=intent.FLAG\u活动\u新建任务或intent.FLAG\u活动\u清除任务
星触觉(意图)
}
}
证书适配器
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)
}
}
package com.example.passwordmanager
导入android.content.Context
导入android.view.LayoutInflater
导入android.view.view
导入android.view.ViewGroup
导入androidx.recyclerview.widget.recyclerview
导入kotlinx.android.synthetic.main.list\u item\u layout.view*
等级证书适应者(
专用val项目:列表,
ctx:Context,val clickListener:(凭证模型)->Unit
):RecyclerView.Adapter(){
var context=ctx
类ViewHolder(itemView:View):RecyclerView.ViewHolder(itemView){
趣味绑定(凭证:凭证模型,单击侦听器:(凭证模型)->Unit){
itemView.urlView.text=凭证.url
itemView.userNameView.text=credential.userName
itemView.passwordView.text=credential.password
itemView.noteView.text=credential.note
itemView.delButton.setOnClickListener{clickListener(凭证)}
}
}
override onCreateViewHolder(父级:ViewGroup,viewType:Int):ViewHolder{
返回ViewHolder(LayoutInflater.from(parent.context)。充气(R.layout.list\u item\u layout,parent,false))
}
重写getItemCount():Int{
返回项目。大小
}
覆盖BindViewHolder(holder:ViewHolder,位置:Int){
val凭证:凭证模型=项目[位置]
holder.bind(凭证,点击监听器)
}
}
在onBindViewHolder中添加或删除setOnClickListener
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.remove.setOnClickListener {
val db = DataBaseHandler(this)
if(db.deleteData(credential.id)){
notifyItemRemoved(holder.getAdapterPosition())
}
}
}
在onBindViewHolder中添加或删除setOnClickListener
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.remove.setOnClickListener {
val db = DataBaseHandler(this)
if(db.deleteData(credential.id)){
notifyItemRemoved(holder.getAdapterPosition())
}
}
}
您正在从数据库中删除该项,但不是从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)
}
}
...
...
}
您正在从数据库中删除该项,但不是从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)
}
}
...
...
}
处理此类情况的最佳方法是使用。 LiveData基本上是一个可观察的类,它只在发生更改时读取数据。 您可以在适配器中创建一个set函数,如:
internal fun setData(data: List<Data>) {
this.data= dataList //this datalist is a list defined in your adapter
notifyDataSetChanged()
}
处理此类情况的最佳方法是使用。 LiveData基本上是一个可观察的类,它只在发生更改时读取数据。 您可以在适配器中创建一个set函数,如:
internal fun setData(data: List<Data>) {
this.data= dataList //this datalist is a list defined in your adapter
notifyDataSetChanged()
}
使用
class AdapterMain(var onClickListener:(Int)->单位):
ListAdapter(DIFFCALBACK){
伴生对象DIFFCALBACK:DiffUtil.ItemCallback(){
覆盖乐趣项相同(旧项:注释,新项:注释):布尔值{
返回oldItem.id==newItem.id
}
覆盖内容相同(旧项:注释,新项:注释):布尔值{
返回oldItem.title==newItem.title&&
oldItem.description==newItem.description&&
oldItem.priority==newItem.priority
}
}
重写CreateViewHolder(父级:ViewGroup,viewType:Int):NoteViewHolder{
val view=LayoutFlater.from(parent.context)。充气(R.layout.note_项,parent,false)
return NoteViewHolder(视图)
}
覆盖BindViewHolder(holder:NoteViewHolder,位置:Int){
holder.txtTitle.text=getItem(position).title
holder.txtDesc.text=getItem(位置)。说明
holder.txtPriority.text=getItem(位置).priority.toString()
}
内部类NoteViewHolder(itemView:View):RecyclerView.ViewHolder(itemView){
var txtTitle:TextView=itemView.txt\u title
var txtDesc:TextView=itemView.txt\u desc
var txtPriority:TextView=itemView.txt\u优先级
初始化{
itemView.setOnClickListener{onClickListener(adapterPosition)}
}
}
fun getNoteAt(位置:Int):注意{
返回项目(位置)
}
}
使用
class AdapterMain(var onClickListener:(Int)->单位):
ListAdapter(DIFFCALBACK){
伴生对象DIFFCALBACK:DiffUtil.ItemCallback(){
覆盖乐趣项相同(旧项:注释,新项:注释):布尔值{
返回oldItem.id==newItem.id
}
覆盖内容相同(旧项:注释,新项:注释):布尔值{
返回oldItem.title==newItem.title&&