【问题标题】:Kotlin adapter remove itemKotlin 适配器删除项目
【发布时间】:2021-05-01 06:26:52
【问题描述】:

我正在尝试从列表中删除项目,但出现以下错误

Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public inline fun <T> MutableCollection<out TypeVariable(T)>.remove(element: TypeVariable(T)): Boolean defined in kotlin.collections
public inline fun <T> MutableList<TypeVariable(T)>.remove(index: Int): TypeVariable(T) defined in kotlin.collections
public inline fun <K, V> MutableMap<out TypeVariable(K), TypeVariable(V)>.remove(key: TypeVariable(K)): TypeVariable(V)? defined in kotlin.collections

Code

class EducationsAdapter(val context: Context?, private var educationList: MyEducations) : RecyclerView.Adapter<EducationsAdapter.EducationsAdapterViewHolder>() {

  override fun getItemCount()= educationList.data.size
  // other functions...


  override fun onBindViewHolder(holder: EducationsAdapter.EducationsAdapterViewHolder, position: Int) 
  {
    holder.educationDelete.setOnClickListener {
      deleteMyEducations(currentItem.id, position)
    }
  }
  
  //delete  
  private fun deleteMyEducations(id: String, position: Int) {
    // ".remove" is returning error above
    educationList.remove(position)
    notifyDataSetChanged()
  }

}

有什么建议吗?

更新

我的MyEducations 类(渲染来自服务器的数据)

data class MyEducations(
    val data: List<Education>,
    val message: String
) { }

data class Education(
    val id: String,
    val start: String,
    val end: String,
    val title: String,
    val body: String,
    val user: User,
    val created_at: String,
    val updated_at: String,
) {}

更新 2

我做了以下更改

// add
val list = mutableListOf<MyEducations>()


private fun deleteMyEducations(id: String, position: Int) {
  //changed to
  list.remove(educationList.data[position])
  notifyDataSetChanged()
}

它的作用是闪烁删除项目(意思:不到一秒钟我的项目删除并再次返回列表)!

【问题讨论】:

  • MyEducations 的类型是什么?它是可变的吗?如果不将其创建为 MutableList 并使用 remove。
  • @mangupt 更新了我的问题
  • val data: List&lt;Education&gt; 是非可变类型。请尝试val data: MutableList&lt;Education&gt;
  • @mangupt Property must be initialized or be abstract 你介意发布答案吗?我可能放错地方了!
  • 将列表类型更改为MutableList 或ArrayList。附带说明一下,您的适配器应该只对 List&lt;Education&gt; 感兴趣,而不是对 MyEducations 感兴趣。 MyEducations 这只是一个包装类,如果你不将它传递给适配器,你会更好地使用它,只需将 myEducations.data 传递给适配器,然后从列表中删除项目。

标签: android kotlin android-recyclerview android-adapter


【解决方案1】:

已解决

//changed my list to `ArrayList<Education>`
class EducationsAdapter(val context: Context?, private var educationList: ArrayList<Education>) : RecyclerView.Adapter<EducationsAdapter.EducationsAdapterViewHolder>() {

  // and my delete code to
  private fun deleteMyEducations(id: String, position: Int) {
    educationList.removeAt(position)
    notifyItemRemoved(position)
    notifyItemRangeChanged(position, educationList.size)
    notifyDataSetChanged()
  }
}

我还必须把我的班级改成ArrayList

data class MyEducations(
    val data: ArrayList<Education>,
    val message: String
) { }

【讨论】:

    【解决方案2】:

    试试

    val list = mutableListOf<MyEducations>()
    
    private fun deleteMyEducations(id: String, position: Int) {
      list.removeAt(position)
      notifyDataSetChanged()
    }
    

    【讨论】:

      【解决方案3】:

      您应该可以使用以下内容删除:

      class EducationListAdapter: RecyclerView.Adapter<EducationListAdapter.EducationViewHolder>() {
          var educationList: MutableList<Education> = mutableListOf()
      
          override fun onBindViewHolder(holder: EducationViewHolder, position: Int) {
              val current = educationList.toList()
              holder.view.setOnClickListener {
                  educationList.remove(current[position])
                  notifyItemRemoved(position)
              }
          }
      }
      

      【讨论】:

      • 您无法从我的适配器中删除, private var educationList: MyEducations,它从我的API调用片段中接收数据。 PS:请看我的update 2
      • @mafortis,如果这对您有用,请接受答案。
      • @mafortis,我的示例 sn-p 是为了告诉如何使用 mutablelist。
      猜你喜欢
      • 2017-09-06
      • 2021-03-03
      • 1970-01-01
      • 2020-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-06
      • 1970-01-01
      相关资源
      最近更新 更多