Search code examples
androidandroid-recyclerviewkotlinrx-kotlin

Populate a RecyclerView with rxkotlin: The Recyclerview stay empty


Here is a kotlin activity which should display a list of events (from sample.json)

class TalksActivity : AppCompatActivity(), TalkAdapter.Listener {

private val TAG = TalksActivity::class.java.simpleName

private var mCompositeDisposable: CompositeDisposable? = null
private var mAdapter: TalkAdapter? = null
private var disposable: Disposable? = null
private val mapper = createMapper()

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_talks)
    pbWaiting.visibility = View.VISIBLE

    mCompositeDisposable = CompositeDisposable()

    initRecyclerView()
    loadTalks()
}


private fun initRecyclerView() {
    rv_talks_list.setHasFixedSize(true)
    val layoutManager: RecyclerView.LayoutManager = LinearLayoutManager(this)
    rv_talks_list.layoutManager = layoutManager
    rv_talks_list.adapter = TalkAdapter(ArrayList(Collections.emptyList()), this)
}

private fun loadTalks() {
    disposable = getTalks()
            .subscribeOn(AndroidSchedulers.mainThread())
            .observeOn(Schedulers.io())
            .subscribe ({ result -> handleResponse(result) }, { error -> handleError(error) })
}

private fun handleResponse(talkList: List<Talk>) {
    mAdapter = TalkAdapter(ArrayList(talkList), this)
    rv_talks_list.adapter = mAdapter
    pbWaiting.visibility = View.GONE
}

private fun handleError(error: Throwable) {
    pbWaiting.visibility = View.GONE
    throw error
}

private fun createMapper(): ObjectMapper {
    val mapper = jacksonObjectMapper()
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
    return mapper
}

override fun onItemClick(talk: Talk) {
    startActivity(CountdownActivity.newIntent(this, talk))
}

private fun getTalks() : Observable<List<Talk>> {
    val text = resources.openRawResource(R.raw.sample).bufferedReader().use { it.readText() }
    val typeFactory = mapper.typeFactory
    val collectionType = typeFactory.constructCollectionType(ArrayList::class.java, Talk::class.java)
    return Observable.create<List<Talk>> {
        mapper.readValue(text, collectionType)
    }
}

}

The problem: When I call loadTalks(), handleResponse(result) or handleError(error) are never called and the screen stay white with only the progressbar running.

I have no error in the console.

Here is my very simple activity_talks.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TalksActivity">

<android.support.v7.widget.RecyclerView
    android:id="@+id/rv_talks_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ProgressBar
    android:id="@+id/pbWaiting"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

What's wrong?

This was working without rxKotlin and observable code.

EDIT

Here is my Adapter:

class TalkAdapter(private val dataList: ArrayList<Talk>, private val listener: Listener) : RecyclerView.Adapter<TalkAdapter.ViewHolder>() {

interface Listener {

    fun onItemClick(talk: Talk)
}

private val colors: Array<String> = arrayOf("#EF5350", "#EC407A", "#AB47BC", "#7E57C2", "#5C6BC0", "#42A5F5")

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    holder.bind(dataList[position], listener, colors, position)
}

override fun getItemCount(): Int = dataList.count()

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {

    val view = LayoutInflater.from(parent.context).inflate(R.layout.adapter_talk, parent, false)

    return ViewHolder(view)
}

class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {

    fun bind(talk: Talk, listener: Listener, colors: Array<String>, position: Int) {

        itemView.title.text = talk.title
        itemView.recap.text = talk.summary
        itemView.eventId.text = talk.eventId
        itemView.setBackgroundColor(Color.parseColor(colors[position % 6]))

        itemView.setOnClickListener { listener.onItemClick(talk) }
    }
}
}

EDIT

Found the solution thanks to Demigod answer.

When I create an Observable with Observable.create I have to trigger onNext() and onError() manually.

To fix it, change

private fun getTalks() : Observable<List<Talk>> {
    val text = resources.openRawResource(R.raw.sample).bufferedReader().use { it.readText() }
    val typeFactory = mapper.typeFactory
    val collectionType = typeFactory.constructCollectionType(ArrayList::class.java, Talk::class.java)
    return Observable.create<List<Talk>> {
        mapper.readValue(text, collectionType)
    }
}

with

private fun getTalks(): Observable<List<Talk>> {
    val text = resources.openRawResource(R.raw.sample).bufferedReader().use { it.readText() }
    val typeFactory = mapper.typeFactory
    val collectionType = typeFactory.constructCollectionType(ArrayList::class.java, Talk::class.java)
    return Observable.fromCallable { mapper.readValue<List<Talk>>(text, collectionType) }
}

and for better perfs:

private fun getTalks(): Observable<List<Talk>> {
    return Observable.fromCallable {
        val text = resources.openRawResource(R.raw.sample).bufferedReader().use { it.readText() }
        val typeFactory = mapper.typeFactory
        val collectionType = typeFactory.constructCollectionType(ArrayList::class.java, Talk::class.java)
        mapper.readValue<List<Talk>>(text, collectionType)
    }
}

Solution

  • I think that the issue is in the way you're creating your Observable:

     return Observable.create<List<Talk>> {
          mapper.readValue(text, collectionType)
     }
    

    When you're creating your observable with Observable.create you should manually emit new items like this:

    Observable.create<Int> { e: ObservableEmitter<Int> ->
        e.onNext(1)
    }
    

    In your case you should probable use Observable.fromCallable { } or Single.fromCallable { } since it will be a single result anyway.