【问题标题】:I can't delete item in recyclerview using room database and coroutines我无法使用房间数据库和协程删除 recyclerview 中的项目
【发布时间】:2021-01-17 14:09:52
【问题描述】:

我是 android jetpack 组件的新手,但作为一个学习者,我想制作一个笔记应用程序,在这个应用程序中,我使用房间数据库和协程。它已成功插入,更新但我无法删除。 这是我的代码,可能很长 显示片段

class ShowFragment : BaseFragment(),onItemClickListener {
private var customerNote:Customer? = null
override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_show, container, false)
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)

    recyclerView.setHasFixedSize(true)
    recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)

    launch {
        context?.let {
            val c = CustomerDatabase(it).getNoteDao().getAllNote()
            recyclerView.adapter = ShowAdapter(c as ArrayList<Customer>,this@ShowFragment)
            it.toast("Show!")

        }
    }

    add_button.setOnClickListener {
        val action = ShowFragmentDirections.actionAddNote()
        Navigation.findNavController(it).navigate(action)
    }
}

override fun onClick(item: Customer, position: Int) {
    launch {
                context?.let {
                    CustomerDatabase(it).getNoteDao().noteDelete(customerNote!!)
                    it.toast("Deleted!")
                }
    }
}

基础片段

abstract class BaseFragment:Fragment(),CoroutineScope {
private lateinit var job:Job

override val coroutineContext: CoroutineContext
    get() = job + Dispatchers.Main

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    job = Job()
}

override fun onDestroy() {
    super.onDestroy()
    job.cancel()
}

显示适配器

class ShowAdapter(private val customerList:ArrayList<Customer>,private val onItemClickListener: onItemClickListener):RecyclerView.Adapter<ShowAdapter.ViewHolder>() {

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

    override fun onBindViewHolder(holder: ShowAdapter.ViewHolder, position: Int) {
        holder.itemView.emName.text = customerList[position].name
        holder.itemView.emPosition.text = customerList[position].position
        holder.itemView.emAddress.text = customerList[position].address
        holder.itemView.emPhone.text = customerList[position].phone


        holder.itemView.emUpdate.setOnClickListener {
            val action = ShowFragmentDirections.actionAddNote()
            action.customer = customerList[position]
            Navigation.findNavController(it).navigate(action)
        }

        holder.itemView.emDelete.setOnClickListener {
            onItemClickListener.onClick(customerList[position],position)
        }

    }


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

    class ViewHolder(itemView: View):RecyclerView.ViewHolder(itemView) {
}

我该如何解决这个问题,请帮助我

【问题讨论】:

    标签: android android-recyclerview android-room


    【解决方案1】:

    在您的ShowFragment 中,您通过传递错误的空对象来调用noteDelete

    所以试试下面的

    override fun onClick(item: Customer, position: Int) {
        launch {
                    context?.let {
                        CustomerDatabase(it).getNoteDao().noteDelete(item)
                        val c = CustomerDatabase(it).getNoteDao().getAllNote()
                        (recyclerView.adapter as ShowAdapter).addNewList(c)
                    }
        }
    
    //add this function in your adapter
    
     fun addnewList(newCustomerList:ArrayList<Customer>){
         customerList = newCustomewList
         notifyDataSetChanged()
     }
    

    【讨论】:

    • 没有错误但该项目仍然没有被删除,你能帮我吗?谢谢@rcs
    • @user10997009 您是否尝试在您的onClick 中传递item 而不是customerNote!!,因为customerNote 为空,这会导致应用程序崩溃。尝试粘贴我的答案,看看它是否有效
    • 看不懂,能解释一下吗?谢谢
    • 您在删除逻辑中引用了错误的对象,尝试将我更新的代码粘贴到您的ShowFragment 中,看看是否有效
    • 是的,我正在传递项目,但它不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-01
    • 2020-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多