Home > Enterprise >  Problems updating recycler views with sqlite database in kotlin
Problems updating recycler views with sqlite database in kotlin

Time:08-21

I am writing a code that deletes the item from the database when the button of the popup menu is pressed , and deletes it according to the recycler viewer. selected item is deleted from the database, but not from the recycler view.

error code: No error code

PrintActivity.kt

class PrintActivity : AppCompatActivity() {
    val helper = SqliteHelper(this, "myDB.sql", 1)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val vBinding = ActivityPrintBinding.inflate(layoutInflater)
        setContentView(vBinding.root)

        var recyclerViewAdapter = CustomAdapter()
        recyclerViewAdapter.listData = helper.select()
        vBinding.myRecyclerView.adapter = recyclerViewAdapter
        vBinding.myRecyclerView.layoutManager = LinearLayoutManager(this)
        vBinding.myRecyclerView.addItemDecoration(
            DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
        )
    }

    inner class CustomAdapter : RecyclerView.Adapter<CustomAdapter.Holder>(){
        var listData = ArrayList<questionType>()
        inner class Holder(val vBinding: QuestionLayoutRecyclerBinding) :
            RecyclerView.ViewHolder(vBinding.root){

                fun setData(id:Int?, question: String, answer: String, exp: String) {
                    vBinding.printId.text=id.toString()

                    vBinding.myLinear.setOnClickListener {
                        var myPopupMenu = PopupMenu(this@PrintActivity, it)
                        menuInflater?.inflate(R.menu.menu, myPopupMenu.menu)
                        var listener = PopupMenuListener()
                        myPopupMenu.setOnMenuItemClickListener(listener)
                        myPopupMenu.show()
                    }
                }

            inner class PopupMenuListener:PopupMenu.OnMenuItemClickListener{
                override fun onMenuItemClick(p0: MenuItem?): Boolean {
                    listData[adapterPosition].id?.let { helper.delete(it) }
                    //here
                    [email protected]()
                    return false
                }
            }
         }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomAdapter.Holder {
            val vBinding = QuestionLayoutRecyclerBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
            return Holder(vBinding)
        }

        override fun onBindViewHolder(holder: Holder, position: Int) {
            val question = listData[position]
            holder.setData(question.id, question.question, question.answer, question.exp)
        }

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

    }

}


CodePudding user response:

You can try this approach using higher order function as callback mechanism. One recommendation I would give is you can try to avoid these nested inner classes and make into separate files. Hope it works

   inner class CustomAdapter : RecyclerView.Adapter<CustomAdapter.Holder>(){
        var listData = ArrayList<questionType>()
    
        fun deleteItem(position: Int) { //Function to remove recyclerview item
            listData.remove(position)
            notifyItemRemoved(position)
            notifyItemRangeChanged(position, getItemCount())
        }
        
        inner class Holder(val vBinding: QuestionLayoutRecyclerBinding) :
            RecyclerView.ViewHolder(vBinding.root){
    
            fun setData(id:Int?, question: String, answer: String, exp: String,itemRemovedCallback:(a:Int)->Unit) { //higher order callback function
                vBinding.printId.text=id.toString()
    
                vBinding.myLinear.setOnClickListener {
                    var myPopupMenu = PopupMenu(this@PrintActivity, it)
                    menuInflater?.inflate(R.menu.menu, myPopupMenu.menu)
                    var listener = PopupMenuListener(itemRemovedCallback) //Passed to popupmenu listener
                    myPopupMenu.setOnMenuItemClickListener(listener)
                    myPopupMenu.show()
                }
            }
    
            inner class PopupMenuListener(val itemRemovedCallback:(a:Int)->Unit):PopupMenu.OnMenuItemClickListener{
                override fun onMenuItemClick(p0: MenuItem?): Boolean {
                    listData[adapterPosition].id?.let { helper.delete(it) }
                    itemRemovedCallback.invoke(adapterPosition) //Calling function. It will pass position to function in adapter
                    return false
                }
            }
        }
    
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomAdapter.Holder {
            val vBinding = QuestionLayoutRecyclerBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
            return Holder(vBinding)
        }
    
        override fun onBindViewHolder(holder: Holder, position: Int) {
            val question = listData[position]
            holder.setData(question.id, question.question, question.answer, question.exp,::deleteItem) //Passing function as higher order function to viewholder
        }
    
        override fun getItemCount(): Int {
            return listData.size
        }
    
    }
  • Related