class AdapterClass:RecyclerView.Adapter<AdapterClass.ViewHolder>() {
var indixepersonas:MutableList<Modello> = ArrayList()
lateinit var context:Context
fun constructor(person: MutableList<Modello>, context: Context) {
this.indixepersonas = person
this.context = context
}
fun remove(position: Int){
indixepersonas.removeAt(position)
notifyItemRemoved(position)
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { //gonfia
val layoutInflater=LayoutInflater.from(parent.context)
return ViewHolder(layoutInflater.inflate(R.layout.items, parent, false))
}
override fun getItemCount(): Int {
return indixepersonas.size
notifyDataSetChanged()
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item=indixepersonas.get(position)
holder.bind(item,context)
}
inner class ViewHolder(view: View):RecyclerView.ViewHolder(view){
val nonbre=view.findViewById(R.id.nonbre)as TextView
val apellido=view.findViewById(R.id.apellido) as TextView
var clikc=view.apply {
setOnLongClickListener {
getItemId()
showContextMenu()
}
}
fun bind(modelar:Modello ,context:Context){
nonbre.text=modelar.nome
apellido.text=modelar.cogonome
}
}
}
MainActivity:
class MainActivity : AppCompatActivity() {
lateinit var mRecyclerView: RecyclerView
val adapter: AdapterClass = AdapterClass()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
getPersonas()
settingRecycler()
registerForContextMenu(mRecyclerView)
}
fun settingRecycler() {
mRecyclerView = findViewById(R.id.recyclerview) as RecyclerView
mRecyclerView.setHasFixedSize(true)
mRecyclerView.layoutManager = LinearLayoutManager(this)
adapter.constructor(getPersonas(), this)
mRecyclerView.adapter = adapter
}
fun getPersonas(): MutableList<Modello> { //crea array
var indixepersonas: MutableList<Modello> = ArrayList()
indixepersonas.add(Modello("angelo", "ferretti"))
indixepersonas.add(Modello("angio", "ferretti"))
indixepersonas.add(Modello("ademaro", "ferretti"))
return indixepersonas
}
override fun onCreateContextMenu(
menu: ContextMenu?,
v: View?,
menuInfo: ContextMenu.ContextMenuInfo?
) {
var m = menuInflater.inflate(R.menu.menu, menu)
super.onCreateContextMenu(menu, v, menuInfo)
}
override fun onContextItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.remover -> {
adapter.remove( item.itemId) }
}
return true }
}
错误:
java.lang.IndexOutOfBoundsException:索引:2131165321,大小:3 ///////超出索引了?
item.itemId没有给我正确的索引?在java.util.ArrayList.remove(ArrayList.java:503)在com.example.delatetext.AdapterClass.remove(AdapterClass.kt:19)在com.example.delatetext.MainActivity.onContextItemSelected(MainActivity.kt:50)在androidx上的android.app.Activity.onMenuItemSelected(Activity.java:3563)androidx.fragment.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:384)androidx.appcompat.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:219) .appcompat.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java
答案 0 :(得分:0)
在tree_delete
中将要删除的索引传递给此处,用您要删除的项目的索引替换Tree *
,以防此adapter.remove(item.itemId)
返回菜单项的自动生成的ID
答案 1 :(得分:0)
您的当机事故发生在第adapter.remove( item.itemId) }
行。原因是您的适配器方法占据一个位置,但是您正在传递菜单项的内部ID。苹果到橘子。由于它看起来只有一个要删除的菜单项,因此需要某种方式知道应删除哪个数据项。也许那是当前列表中的最后一项,或者它需要用户输入。不管您确定了什么,都将适当的位置传递到适配器。
注意:还应使用remove
方法修改适配器。目前,它会将应用程序暴露给ConcurrentModificationException
。 (想象一下,如果有人在调用remove方法时正在滚动列表-适配器可能会尝试绑定不存在的数据)。这是另一种方法:
fun remove(position: Int){
val newList = ArrayList(indixepersonas).also { it.removeAt(position) }
indixepersonas = newList
notifyItemRemoved(position)
}
请注意,我也删除了notifyDataSetChanged()
调用,因为这与notifyItemRemoved
调用是多余的。