3

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)
    }
}
psv
  • 3,147
  • 5
  • 32
  • 67

3 Answers3

1

You need to change your code. You are subscribeOn mainThered instead of background thread. Please below code:-

 disposable = getTalks()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe ({ result -> handleResponse(result) }, { error -> handleError(error) })
Anand Jain
  • 2,365
  • 7
  • 40
  • 66
  • Thank you, this is a good observation. I changed it but `handleResponse(result)` or `handleError(error)` are still not called – psv Oct 11 '18 at 10:00
1

This should be the code. I hope this helps.

disposable = getTalks()
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribeBy (onNext = { handleResponse(it) }, onError = { handleError(it)}
Indranil Dutta
  • 159
  • 2
  • 14
1

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.

Demigod
  • 5,073
  • 3
  • 31
  • 49
  • I understand what you mean, but when I change ```return Observable.create> { mapper.readValue(text, collectionType) }``` with ```return Observable.fromCallable(mapper.readValue(text, collectionType))```, I get an error: ```Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.util.concurrent.Callable``` – psv Oct 11 '18 at 12:21
  • @psv Please change it like : `Observable.fromCallable { mapper.readValue(text, collectionType) } ` – Demigod Oct 11 '18 at 12:36
  • I tried, and the compiler complains: ```Type inference failed: Not enough information to infer parameter T in fun readValue(p0: String!, p1: JavaType!): T! Please specify it explicitly.``` (`readValue` is underlined in red) – psv Oct 11 '18 at 12:41
  • @psv, I see. What is the signature of `readValue` method? Actually, I think you can do something like : `Observable.fromCallable { mapper.readValue>(text, collectionType) }` – Demigod Oct 11 '18 at 12:45
  • the signature is `public T readValue(String content, JavaType valueType)`. YES It works! Thank you Demigod. I was not far; I wrote something which was working as well: ```return Observable.fromCallable { val result: List = mapper.readValue(text, collectionType) result } ``` But your solution is even better! Thanks – psv Oct 11 '18 at 13:01