- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
这是一个 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/
我的新 Nexus One 预装了 CyanogenMod。到目前为止它很棒,但由于我出于软件开发目的购买它(最初只是学习如何为 Android 编程),我应该继续使用它,还是安装官方 Android
在附带的笔中,我想编写我的代码,以便一旦用户将“选择设计”悬停在上方,选项就会保留,不会在用户将鼠标移开后立即消失。 https://codepen.io/anon/pen/EGNGdR 这是我尝试过
我有 CSS 问题。 这是 JsFiddle:http://jsfiddle.net/63NHM/1/ 问题是标题下的正文不会自动换行,他“停留”在一行中,这不好。 这是本节的CSS #trackde
我正在尝试制作自定义控件库。起初我以为我已经完成了表格并且完全可以工作,但我没有。我不会询问如何解决我的表单问题,而是使用我的按钮询问。 我正在制作一个自定义按钮,它可以根据每个状态进行广泛自定义,它
我有 Mysql 5.5我创建了一个存储过程 CREATE DEFINER=`root`@`%` PROCEDURE `refresh_mobileTemp`() BEGIN DROP TABLE I
有没有办法让 Java 应用程序的框架停留在我用 .setLocation 让它出现的位置?我想禁用它被用户在屏幕上移动的能力。 最佳答案 以下是防止移动 JFrame 的方法。在生产应用程序中使用它
我已经尝试了很多次迭代,但它似乎不想只停留在最后一个关键帧上。 我不确定我在这里做错了什么。 #container{ position: relative; width: 100%;
我正在尝试生成一个 svg 矩形和一个 span,以便 span 元素中的文本与 svg 元素垂直对齐。在声明式 HTML+CSS 中,我设法做到了。但我无法通过 javascript 做到这一点。
我目前正在努力使我的伪元素 :before 在单击时保持事件状态。 我在这里做了一个小演示: https://jsfiddle.net/ud8p2nak/1/ 我希望它在当前页面上保持事件状态,所以如
当我指向主菜单时,我得到了我的颜色: 但是当我指向子菜单项时,菜单更改为默认颜色而不是我的颜色。 我的 CSS: .menu:hover { -fx-background-color: #D5
我想在我的 jsp 登录页面中实现保持登录状态或记住我。我正在使用基于容器的表单例份验证。我想我需要将用户的数据(例如 userid 和 token)存储到 cookie 中,这是一种状态信息,用于确
我有一个程序,其中我全局定义的静态变量一旦离开我的“初始化函数”(不是构造函数)就不会保持初始化状态。这是该程序: type.h namespace type { static int * s
在 Delphi XE Update 1 中,如果父(主)表单的 FormStyle 设置为 fsStayOnTop,我会得到模态表单看似随机的行为。 1) 使用 MainFormOnTaskbar
在大多数网站上,当用户要提供用户名和密码登录系统时,都会有一个复选框,例如“保持登录状态”。如果您选中该框,您将在同一 Web 浏览器的所有 session 中保持登录状态。如何在 Java EE 中
我查找了一些有关此警告的答案,但它们既没有帮助我,也没有真正理解 Perl 在这里所做的事情。这就是我想要它做的事情: sub outerSub { my $dom = someBigDOM;
如何在页面加载时显示 qTip 标注,并且即使我将鼠标悬停在该字段上也保留在那里。本质上,我只想要一个固定的标注,并且使用 qTip 来简化定位和样式设置。 注意:我尝试了“show: {ready:
我有一个使用 WorkflowApplication 在 IIS 中托管 WF4 工作流的应用程序 工作流由用户定义(使用重新托管的工作流设计器)并且 xml 存储在数据库中。然后,根据使用该应用程序
所以这真的让我很烦,我不知道这是浏览器相关的故障还是 javascript 就是这样工作的(我希望是这样)。我创建了一个 fiddle 。 https://jsbin.com/laluziqede/1
我已经用 CSS3/javascript 做了一个幻灯片演示,但这不能正常工作.. 当您单击箭头时,“diapos”div 会朝一个方向移动,但在我的例子中,框内的文本并没有完全移动,一小部分文本留在
我希望所有非 EN 的访问者都能被重定向,但来自 EN 的访问者应该留在网站上。有命令吗? var country= geoip_country_code(); if(country =
我是一名优秀的程序员,十分优秀!