3
  • notifyDatasetChanged() is not working, But instead of notifydatasetChanged(), if i pass the eventList with data to adapter directly after initializing recyclerview list loads fine.
  • How to resolve this

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!!
}
Android
  • 1,420
  • 4
  • 13
  • 23
Devrath
  • 42,072
  • 54
  • 195
  • 297
  • Could you try `eventList.clear()` and then `eventList.addAll(it.personName.getmData())` ? – John Le Dec 25 '18 at 04:51
  • Redman answered your question :D. The main reason is that the one(in Activity) and the one(in Apdapter) didn't keep the same reference of the list – John Le Dec 25 '18 at 04:59
  • Does this answer your question? [Android: notifyDataSetChanged(); not working](https://stackoverflow.com/questions/16165728/android-notifydatasetchanged-not-working) – Sazzad Hissain Khan Nov 20 '19 at 04:07

1 Answers1

2

Recyclerview notifyDatasetChanged() will work only until you don't change the reference of the list . But in your receiver you might be changing reference to the list

private fun setRxJavaRecievers() {

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

so instead do

 private fun setRxJavaRecievers() {

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

or set the adapter again to make it work

Manohar
  • 22,116
  • 9
  • 108
  • 144