gpt4 book ai didi

android - notifyDatasetChanged() 在 Kotlin 中不起作用

转载 作者:行者123 更新时间:2023-11-29 00:57:05 26 4
gpt4 key购买 nike

  • notifyDatasetChanged() 不工作,而是notifydatasetChanged(),如果我将带有数据的eventList传递给适配器在初始化 recyclerview 列表后直接加载正常。
  • 如何解决这个问题

ActEvents.kt

class ActEvents : AppCompatActivity(){

// Initializing an empty ArrayList to be filled with animals
var eventList: MutableList<TestModel> = ArrayList()

@set:Inject var retrofit: Retrofit? = null

private lateinit var personDisposable: Disposable

/******************* Life Cycle Methods *************************/
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
initOnCreate()
}

override fun onDestroy() {
super.onDestroy()
initOnDestroy()
}

override fun onSupportNavigateUp(): Boolean {
onBackPressed()
return true
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
initOnCreateOptionsMenu(menu)
return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean { return initOnOptionsItemSelected(item) }
/******************* Life Cycle Methods *************************/


/******************** Subscribers *******************************/
private fun setRxJavaRecievers() {

personDisposable = RxBus.listen(RxEvent.RxEventList::class.java).subscribe {
eventList = it.personName.getmData()
event_list.adapter!!.notifyDataSetChanged()
}
}
/******************** Subscribers *******************************/

/******************* Init Methods *******************************/
/** Init On Create **/
private fun initOnCreate() {
//Set up the UI
setContentView(R.layout.act_events)
//Inject Dagger Component
(application as CaringApp).netComponent.inject(this)
//Set up toolbar
setToolbar()
//Set up the recycler view
initRecyclerView()
//Set Rx java receivers
setRxJavaRecievers()

}



/** Init On Destroy **/
private fun initOnDestroy() {
//Un Register disposable
if (!personDisposable.isDisposed) personDisposable.dispose()
}


/** Initialize recyclerView **/
private fun initRecyclerView() {
val mLayoutManager = LinearLayoutManager(applicationContext)
event_list.layoutManager = mLayoutManager
event_list.itemAnimator = DefaultItemAnimator()
event_list.adapter = AdptEvents(eventList,this)
}


/******************* Init Methods *******************************/

}

AdptEvents.kt

class AdptEvents (val items: MutableList<TestModel>, val context: Context) : RecyclerView.Adapter<ViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, p1: Int): ViewHolder {
return ViewHolder(LayoutInflater.from(context).inflate(R.layout.row_event, parent, false))
}

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

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.tvAnimalType.text = items[position].getName()
}

}

class ViewHolder (view: View) : RecyclerView.ViewHolder(view) {
// Holds the TextView that will add each animal to
val tvAnimalType = view.txtTitle!!
}

最佳答案

Recyclerview notifyDatasetChanged() 只有在您不更改列表的引用时才会起作用。但是在您的接收器中,您可能正在更改对列表的引用

private fun setRxJavaRecievers() {

personDisposable = RxBus.listen(RxEvent.RxEventList::class.java).subscribe {
eventList = it.personName.getmData()
event_list.adapter!!.notifyDataSetChanged()
}
}

所以而不是做

 private fun setRxJavaRecievers() {

personDisposable = RxBus.listen(RxEvent.RxEventList::class.java).subscribe {
eventList.clear()
eventList.addAll(it.personName.getmData())
event_list.adapter!!.notifyDataSetChanged()
}
}

或重新设置适配器使其工作

关于android - notifyDatasetChanged() 在 Kotlin 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53919399/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com