Kotlin 适配器删除项目
Kotlin adapter remove item
我试图从我的列表中删除项目,但出现以下错误
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
class(渲染数据来自服务器)
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()
}
它所做的是快速删除项目(意思:不到一秒钟我的项目被删除并再次返回列表)!
您应该可以使用以下内容进行删除:
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)
}
}
}
试试
val list = mutableListOf<MyEducations>()
private fun deleteMyEducations(id: String, position: Int) {
list.removeAt(position)
notifyDataSetChanged()
}
已解决
//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()
}
}
此外,我还必须将 class 更改为 ArrayList
data class MyEducations(
val data: ArrayList<Education>,
val message: String
) { }
我试图从我的列表中删除项目,但出现以下错误
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
class(渲染数据来自服务器)
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()
}
它所做的是快速删除项目(意思:不到一秒钟我的项目被删除并再次返回列表)!
您应该可以使用以下内容进行删除:
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)
}
}
}
试试
val list = mutableListOf<MyEducations>()
private fun deleteMyEducations(id: String, position: Int) {
list.removeAt(position)
notifyDataSetChanged()
}
已解决
//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()
}
}
此外,我还必须将 class 更改为 ArrayList
data class MyEducations(
val data: ArrayList<Education>,
val message: String
) { }