Eugene V.
Eugene V.

Reputation: 100

RecyclerView adapter doesn't update data with databinding

Please help me with a problem. I have activity.xml file with recyclerView which I set an adapter like:

<androidx.recyclerview.widget.RecyclerView
          android:id="@+id/recyclerView"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="@color/white"
          android:orientation="vertical"
          android:visibility="@{viewModel.recyclerVisibility}"
          app:adapter="@{viewModel.adapter}"
          app:layout_constraintTop_toBottomOf="@id/title" />

So I set the adapter here in xml but it is not updating after I receive data from server in my viewModel:

someObservable.observeOn(AndroidSchedulers.mainThread()).subscribe {
     adapter.setItems(it)
     notifyPropertyChanged(BR.adapter)
})

To bind adapter in xml and have an access to it I have the method in my viewModel like:

@Bindable
fun getAdapter(): SomeAdapter {
    if (adapter == null) {
        adapter = SomeAdapter(emptyList())
    }
    return adapter as SomeAdapter
}

And in my adapter I have method to update datalist:

fun setItems(someData: List<SomeData>) {
    this.someData = someData
    notifyDataSetChanged()
}

So what the problem here? because I see the empty adapter on UI so I guess the binding doesn't work fine..

Upvotes: 0

Views: 994

Answers (1)

Eugene V.
Eugene V.

Reputation: 100

It was a silly but not obvious mistake but the problem is solved :) I forgot to add layout manager for my recyclerView like:

app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"

in xml or you can do it programmatically.. **it happens :)

Upvotes: 1

Related Questions