gpt4 book ai didi

android - 使用 rxkotlin : The Recyclerview stay empty 填充 RecyclerView

转载 作者:搜寻专家 更新时间:2023-11-01 08:19:49 25 4
gpt4 key购买 nike

这是一个 kotlin Activity ,它应该显示事件列表(来自 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(result) 或 handleError(error) 从未被调用,屏幕保持白色,只有进度条在运行。

我在控制台没有错误。

这是我非常简单的 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) }
}
}
}

编辑

感谢 Demigod answer 找到了解决方案。

当我使用 Observable.create 创建一个 Observable 时,我必须手动触发 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)
}
}

最佳答案

我认为问题在于您创建 Observable 的方式:

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

当您使用 Observable.create 创建可观察对象时,您应该像这样手动发出新项目:

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

在您的情况下,您可能应该使用 Observable.fromCallable { }Single.fromCallable { },因为无论如何它都是一个结果。

关于android - 使用 rxkotlin : The Recyclerview stay empty 填充 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52756197/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com