Вот действие kotlin, которое должно отображать список событий (из 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)
}
}
}
Проблема: Когда я вызываю loadTalks()
, handleResponse (результат) или handleError (ошибка)никогда не вызывается, и экран остается белым только с индикатором выполнения.
У меня нет ошибки в консоли.
Вот мой очень простой 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>
Что не так?
Это работало без rxKotlin и наблюдаемого кода.
РЕДАКТИРОВАТЬ
Вот мой адаптер:
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) }
}
}
}
РЕДАКТИРОВАТЬ
Нашел решение благодаря ответу Полубога.
Когда я создаю Observable с Observable.create
, мне нужно вызвать onNext()
и onError()
вручную.
Чтобы исправить это, измените
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)
}
}
на
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) }
}
и для лучшего качества:
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)
}
}