嗨,我在使用翻新和rxjava从服务器调用数据并将其显示在我的布局上时遇到一些麻烦。我是Kotlin开发的新手,所以有点令人困惑。我的问题是我能够从服务器调用数据,但是当我尝试在布局上显示数据时,它什么也没显示。这是我的代码:
我的模特:
data class StackOverFlowCoba(
@field:SerializedName("quota_max")
val quotaMax: Int? = null,
@field:SerializedName("quota_remaining")
val quotaRemaining: Int? = null,
@field:SerializedName("has_more")
val hasMore: Boolean? = null,
@field:SerializedName("items")
val items: List<ItemsItem?>? = null
)
我的主持人:
class MainPresenter : BasePresenter<StackOverFlowView> {
var modelView : StackOverFlowView?=null
val compositeDisposable = CompositeDisposable()
override fun onAttach(view: StackOverFlowView) {
modelView = null
}
override fun onDettach() {
modelView = null
}
fun getData()
{
compositeDisposable.add(RetrofitHttp.stackOverFlow.getdata().observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.newThread())
.subscribe({
t: StackOverFlowCoba? -> modelView?.onSuccess(t?.items)
}
,{t: Throwable? -> modelView?.onError()})
)
}
}
我的View界面:
interface StackOverFlowView : BaseView {
fun onSuccess(data : List<ItemsItem?>?)
fun onError()
}
我的活动代码:
class Main : AppCompatActivity(),StackOverFlowView {
lateinit var mainPresenter : MainPresenter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.coba_stackoverflow)
initPresenter()
toast("something adajfaosdpa")
}
private fun initPresenter()
{
mainPresenter = MainPresenter()
mainPresenter.getData()
}
override fun onSuccess(data: List<ItemsItem?>?) {
user_id.text = data!![0]!!.owner!!.displayName
toast("something basdsadjk")
}
override fun onError() {
toast("something error")
}
override fun onAttachView() {
mainPresenter.onAttach(this)
mainPresenter.getData()
}
override fun onDettachView() {
mainPresenter.onDettach()
}
override fun onStart() {
onAttachView()
super.onStart()
}
override fun onStop() {
onDettachView()
super.onStop()
}
}
当我在logcat上检查它时,一切都很好,我的代码可以从服务器调用数据,但是我不知道为什么我无法在布局上显示它。请帮助我,谢谢你!