如何使用房间从 recyclerview 中删除一行?

How do I remove a row from recyclerview using room?

我正在尝试删除一行 recyclerview 使用 room.im 滑动以删除特定行....

这是我的地址table-->

@Entity(tableName = "address")
 class Address {
@PrimaryKey(autoGenerate = true)
var id = 0

@ColumnInfo(name = "address")
var address: String? = null
 }

地址道:

@Dao
interface AddressDao {
@Insert
suspend fun addData(address: Address)

@Query("select * from address")
fun getAddressesWithChanges() :LiveData<MutableList<Address>>

@Query("SELECT EXISTS (SELECT 1 FROM address WHERE id=:id)")
suspend fun isAddressAdded(id: Int): Int

@Delete
suspend fun delete(address: Address)
  }

数据库:

@Database(entities = [Address::class], version = 1)
abstract class Database : RoomDatabase() {
abstract fun AddressDao(): AddressDao
 } 

地址活动:

class AddressActivity : AppCompatActivity() {
    private val adapter = AddressAdapter()

    private lateinit var data: LiveData<MutableList<Address>>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.address)

        addbutton.findViewById<View>(R.id.addbutton).setOnClickListener {
            val intent = Intent(this, AddAddressActivity::class.java)
            startActivity(intent)
        }

        val recyclerView = findViewById<RecyclerView>(R.id.recyclerview)
        recyclerView.setHasFixedSize(true)
        recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        recyclerView.adapter = adapter
        recyclerView.addItemDecoration(DividerItemDecorator(resources.getDrawable(R.drawable.divider)))
        recyclerView.addOnScrollListener(object :
            RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                Log.e("RecyclerView", "onScrollStateChanged")
            }

            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
            }
        })


        val application = application as CustomApplication
        data = application.database.AddressDao(). getAddressesWithChanges()
        data.observe(this, Observer { words1 ->
            // Update the cached copy of the words in the adapter.
            words1?.let { adapter.updateData(it) }})


    }
}

适配器:

class AddressAdapter: RecyclerSwipeAdapter<AddressAdapter.ViewHolder>() {
    private var addresses: MutableList<Address> = Collections.emptyList()
    lateinit var  Database:Database

    override fun onCreateViewHolder(viewGroup: ViewGroup, itemViewType: Int): ViewHolder =
        ViewHolder(LayoutInflater.from(viewGroup.context).inflate(R.layout.address_item, viewGroup, false))

    override fun getSwipeLayoutResourceId(position: Int): Int {
        return R.id.swipe;
    }

    override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
        val fl: Address = addresses[position]
        viewHolder.tv.setText(fl.address)
        viewHolder.swipelayout.setShowMode(SwipeLayout.ShowMode.PullOut)
        // Drag From Right

        // Drag From Right
        viewHolder.swipelayout.addDrag(
            SwipeLayout.DragEdge.Right,
            viewHolder.swipelayout.findViewById(R.id.bottom_wrapper)
        )
        // Handling different events when swiping
        viewHolder.swipelayout.addSwipeListener(object : SwipeLayout.SwipeListener {
            override fun onClose(layout: SwipeLayout) {
                //when the SurfaceView totally cover the BottomView.
            }

            override fun onUpdate(layout: SwipeLayout, leftOffset: Int, topOffset: Int) {
                //you are swiping.
            }

            override fun onStartOpen(layout: SwipeLayout) {}
            override fun onOpen(layout: SwipeLayout) {
            }

            override fun onStartClose(layout: SwipeLayout) {}
            override fun onHandRelease(
                layout: SwipeLayout,
                xvel: Float,
                yvel: Float
            ) {
            }
        })


        viewHolder.tvDelete.setOnClickListener(View.OnClickListener { view ->
            mItemManger.removeShownLayouts(viewHolder.swipelayout)
            addresses.removeAt(position)

           //What should i do here??????????????????????????
           // val address = Address()

           // Database.AddressDao().delete(address)
            notifyDataSetChanged()
            notifyItemRemoved(position)
            notifyItemRemoved(position)
            notifyItemRangeChanged(position, addresses.size)
            mItemManger.closeAllItems()
            Toast.makeText(
                view.context,
                "Deleted " + viewHolder.tv.getText().toString(),
                Toast.LENGTH_SHORT
            ).show()
        })


        // mItemManger is member in RecyclerSwipeAdapter Class


        // mItemManger is member in RecyclerSwipeAdapter Class
        mItemManger.bindView(viewHolder.itemView, position)
    }


    override fun getItemCount(): Int = addresses.size

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var tv: TextView
        val swipelayout: SwipeLayout
        val tvDelete:TextView



        init {
            tvDelete=itemView.findViewById(R.id.tvDelete)

            tv = itemView.findViewById(R.id.ftv_name)
            swipelayout=itemView.findViewById(R.id.swipe)

        }    }

    fun updateData(addresses:
                   MutableList<Address>) {
        this.addresses = addresses
        notifyDataSetChanged() // TODO: use ListAdapter if animations are needed
    }

}

根据上面的代码,我可以立即删除一行,但是当我重新访问 activity 时,它再次显示已删除的行

我想知道如何使用 onBindviewHolder

删除存储在房间中的数据

根据@quealegriamasalegre 建议的最新答案

这是我的自定义应用程序:--

class CustomApplication: Application() {
    lateinit var database: Database
        private set
    lateinit var addressDao: AddressDao
        private set

    override fun onCreate() {
        super.onCreate()

        this.database = Room.databaseBuilder<Database>(
            applicationContext,
            Database::class.java, "database"
        ).build()
        addressDao = database.AddressDao()

    }
}

现在适配器:

      viewHolder.tvDelete.setOnClickListener(View.OnClickListener { view ->
        mItemManger.removeShownLayouts(viewHolder.swipelayout)
        addresses.removeAt(position)

        val application = CustomApplication()

        application.database.AddressDao().deleteAddress(position)//here you delete from DB so its gone for good
        //notifyDataSetChanged() dont do this as it will reexecute onbindviewholder and skip a nice animation provided by android
        //notifyItemRemoved(position) execute only once


        notifyDataSetChanged()
        notifyItemRemoved(position)
        notifyItemRemoved(position)
        notifyItemRangeChanged(position, addresses.size)
        mItemManger.closeAllItems()
        Toast.makeText(
            view.context,
            "Deleted " + viewHolder.tv.getText().toString(),
            Toast.LENGTH_SHORT
        ).show()
    })

现在因 kotlin.UninitializedPropertyAccessException 而崩溃:lateinit 属性 数据库尚未初始化

真的需要帮助....

你能试试这个吗:

viewHolder.tvDelete.setOnClickListener(View.OnClickListener { view ->
    // this code is to have access to the database inside adapter you should find another way to do it,
    // like pass database in contructor for ex...
    val application = application as CustomApplication
    Database = application.database.AddressDao()
   // main code to delete in db
   GlobalScope.launch(Dispatchers.IO) {
    Database.delete(addresses[position])
   }
  //

    mItemManger.removeShownLayouts(viewHolder.swipelayout)
    addresses.removeAt(position)
....

在地址 DAO 中

@Query("DELETE FROM address WHERE id=:id")
fun deleteAddress(id: Int):Int

然后在你滑动操作完成的位置调用它。

更新:

创建一个类似

的界面
interface ListItemClick{
    fun onClick(int id)
}

在您的地址上实施 activity class 并将此接口传递给适配器。

fun updateData(addresses:MutableList<Address>, listener:ListItemClick) {
        this.addresses = addresses
        this.listener = listener
        notifyDataSetChanged()
}

然后,在您实现滑动删除的地方,调用

listener.onClick(item_id)

在ListItemClick接口的onClick里面的AddressActivity

val application = application as CustomApplication
data = application.database.AddressDao().deleteAddress(id)

我在这里写的,所以可能还有一些拼写错误。但是,我认为你已经掌握了基本的想法。

问题是您只删除了 Kotlin/Java 中的行,而不是 SQLite 房间数据库中的行。我想您应该能够通过简单地添加一行删除数据库中的地址来解决您的问题。这是我如何为您的 tvDelete 可点击视图执行此操作的示例:

val application = application as CustomApplication
    

viewHolder.tvDelete.setOnClickListener(View.OnClickListener { view ->
        mItemManger.removeShownLayouts(viewHolder.swipelayout)
        addresses.removeAt(position)

        //remember that in onBind you already saved the current Address as "fl"

        application.database.AddressDao().delete(fl)//here you delete from DB so its gone for good
        //notifyDataSetChanged() dont do this as it will reexecute onbindviewholder and skip a nice animation provided by android
        notifyItemRemoved(position)
        //notifyItemRemoved(position) execute only once
        notifyItemRangeChanged(position, addresses.size)//i dont think you will need this
        mItemManger.closeAllItems()
        Toast.makeText(
            view.context,
            "Deleted " + viewHolder.tv.getText().toString(),
            Toast.LENGTH_SHORT
        ).show()
    })

所以我猜你已经很接近了。请注意,我在 java 中编写代码,所以我不太相信我的所有代码都是正确的,但你明白了。

正如其他一些回复所说,您的主要错误是您没有在数据库上调用 delete。如果您的数据集基于房间数据库,则从您的适配器中删除项目不会有效,您必须删除实际条目。

您不想从所有代码调用您的数据库,因此您需要创建一个接口作为对您的地址的回调Activity,然后删除电话给你。在适配器中定义接口本身,因为任何使用该适配器的 activity 都应该引用并实现该接口。

在地址适配器中:

interface OnItemSwipeListener {
    fun onItemSwipe(address: Address)
}

然后在AdapterActivity中实现接口和函数

class AddressActivity : AppCompatActivity(), AddressAdapter.OnItemSwipeListener {

    ...

    override fun onItemSwipe(address: Address) {
        application.database.AddressDao().delete(address)
    }

有了这段代码,您需要做两件事来将 AddressActivity 中的实现连接到 AddressAdapter:1) 将 AddressActivity 传递到 AddressAdapter 和 2) 调用滑动事件的侦听器。

在AddressActivity中,将Adapter的实例化移到onCreate(...)方法中

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.address)
        
        val adapter = AddressAdapter(this)

最后,在 AddressAdapter 中,重新配置构造函数以获取侦听器,而不是从适配器中删除项目调用侦听器

class AddressAdapter(val onItemSwipeListener: OnItemSwipeListener): RecyclerSwipeAdapter<AddressAdapter.ViewHolder>() {
        
        ...
        // addresses.removeAt(position)
        onItemSwipeListener.onItemSwipe(address.get(position))

这应该适合你。

可以帮助您的是为您的项目提供更好的结构。 Google 推荐 MVVM 架构,并为他们的项目提供存储库。项目中已有空间和实时数据很好,但添加的结构将帮助您更有效地以更易于理解的方式促进对数据的更改。您不希望在您的代码中到处调用您的数据库,因此这也有助于减少您的数据库访问。

更新: 我注意到您说您正在使用滑动来删除行,但从您的代码看来您实际上是在使用点击事件。我的解决方案的语言可能与您想要命名的内容不一致,但它应该都一样。

要考虑的其他事项是扩展 ListAdapter 而不是您当前使用的刷卡适配器。然后,无需在适配器中实现滑动逻辑,您可以在 Activity 中设置滑动处理程序,在其中初始化适配器和回收器视图。

In AddressActivity(我知道如何在 Java 中执行此操作,您可能需要使用 Kotlin 语法,因为这看起来很有趣)

val itemTouchHelper = ItemTouchHelper( object : ItemTouchHelper.SimpleCallback(0,
            ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) {
        override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
            // do nothing
            return false
        }

        override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
            application.database.AddressDao().delete(adapter.getAddressAt(viewHolder.getAdapterPosition())
        }
    }).attachToRecyclerView(mPostRecyclerView)

您必须在适配器中创建 getAddressAt(pos: Int) 函数,或使用您喜欢的任何方法从适配器中获取正确的地址对象。

按照我的简单步骤解决您的问题,

第一步:AndroidManifest.xml

中检查一次 CustomApplication 名字是否被提及
<application
    android:name=".CustomApplication"

否则你会遇到这个问题

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.AddressActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to com.example.myapplication.CustomApplication

第 2 步: 检查你的模块级别 build.gradle 文件

应用此更改

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

检查依赖项——对于 Kotlin,使用 kapt 而不是 annotationProcessor

implementation "androidx.room:room-runtime:2.2.5"
kapt "androidx.room:room-compiler:2.2.5"

否则你会遇到这个问题

java.lang.RuntimeException: cannot find implementation for com.example.myapplication.Database. Database_Impl does not exist

第 3 步: 检查你的AddressDao接口,添加这个功能

 @Delete
 suspend fun deleteAddress(address: Address)

第 4 步:

AddressAdapterclass中添加这个监听器,

interface ItemListener {
    fun onItemClicked(address: Address, position: Int)
}

添加监听变量和setListener函数

private lateinit var listener: ItemListener

interface ItemListener {
    fun onItemClicked(address: Address, position: Int)
}

fun setListener(listener: ItemListener) {
    this.listener = listener;
}

然后在 tvDelete.setOnClickListener 方法中更新您的代码

    viewHolder.tvDelete.setOnClickListener(View.OnClickListener { view ->
        mItemManger.removeShownLayouts(viewHolder.swipelayout)
        addresses.removeAt(position)

        listener.onItemClicked(fl, position)
        
        notifyDataSetChanged()
     //   notifyItemRemoved(position)
     //   notifyItemRangeChanged(position, addresses.size)
        mItemManger.closeAllItems()
        Toast.makeText(
            view.context,
            "Deleted " + viewHolder.tv.getText().toString(),
            Toast.LENGTH_SHORT
        ).show()
    })

第 5 步:AddressActivity class 中,执​​行此更改

在这里实现监听器,

class AddressActivity : AppCompatActivity(), AddressAdapter.ItemListener {

然后覆盖方法

override fun onItemClicked(address: Address, position: Int) {
    
}

然后为适配器设置侦听器

        recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        recyclerView.adapter = adapter
        adapter.setListener(this)

然后更新覆盖方法中的代码

override fun onItemClicked(address: Address, position: Int) {
    lifecycleScope.launch {
        val application = application as CustomApplication
        application.database.AddressDao().deleteAddress(address)
    }
}

这里我使用了协程,否则你也可以使用 AsycTask

对于协同程序,将此依赖项添加到您的模块 build.gradle 文件

implementation "android.arch.lifecycle:extensions:1.1.1"
kapt "android.arch.lifecycle:compiler:1.1.1"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.2.0"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0'

如果您在 UI class 中直接调用 deleteAddress 方法,就会遇到此问题

java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.

所以在后台线程中使用这些方法,

如果你真的想在主线程中执行 UI,请在你的代码中进行此更改

AddressDao界面,

@Delete
fun deleteAddress(address: Address)

CustomApplicationclass中添加allowMainThreadQueries()

class CustomApplication : Application() {
    lateinit var database: Database
        private set
    lateinit var addressDao: AddressDao
        private set

    override fun onCreate() {
        super.onCreate()

        this.database = Room.databaseBuilder<Database>(
            applicationContext,
            Database::class.java, "database"
        ).allowMainThreadQueries().build()
        addressDao = database.AddressDao()
    }
}