ameya
ameya

Reputation: 301

How to update recycleView in android after deleting item in kotlin?

I have created a recycle view and inside that using card view for items. I have a delete button inside a card view whenever I click on that button my item is deleted from SQLite database. But to reflect it on UI, app need to restart. How can I notify adpater that item is deleted?

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

Upvotes: 0

Views: 2417

Answers (5)

Allory
Allory

Reputation: 1

class coba : AppCompatActivity() { private lateinit var recycleView :RecyclerView private lateinit var datalis :ArrayList override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_coba) recycleView = findViewById(R.id.rcycoba) datalis = ArrayList()

    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
}

}

Upvotes: 0

mohosyny
mohosyny

Reputation: 1042

use 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..

Upvotes: 0

Haris Bin Saif
Haris Bin Saif

Reputation: 78

The best way to handle these kinds of situations is to use LiveData. LiveData is basically an observable class which reads data only when there is a change. What you can do is create a set function in your adapter like:

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

now in your main activity/fragment, create a LiveData List outside the onCreate function like this:

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

Now inside your onCreate function, use can observe the livedata and set the data for recyclerview like this:

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

Upvotes: 1

Hussain
Hussain

Reputation: 1335

You are deleting the item from database but not from the list inside recyclerview adapter.

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

    ...
    ...
}

Inside MainActivity.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()
    }
}

Upvotes: 0

S T
S T

Reputation: 1134

add remove setOnClickListener in your onBindViewHolder.

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

    holder.remove.setOnClickListener {

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

    }
}

Upvotes: 1

Related Questions