Android Recylerview MVVM中未显示JSON数据
我正在尝试使用数据绑定在Recyclerview中解析Json。当我运行应用程序时,它只显示空白屏幕,没有错误/崩溃 Json数据: { “味精”:[ “足球”, “板球”, “棒球”, “橄榄球”, “海湾” ], “状态”:“成功” } ApiAndroid Recylerview MVVM中未显示JSON数据,android,kotlin,data-binding,retrofit2,dagger-2,Android,Kotlin,Data Binding,Retrofit2,Dagger 2,我正在尝试使用数据绑定在Recyclerview中解析Json。当我运行应用程序时,它只显示空白屏幕,没有错误/崩溃 Json数据: { “味精”:[ “足球”, “板球”, “棒球”, “橄榄球”, “海湾” ], “状态”:“成功” } Api interface SportsApi { /** * Get the Sports from the API */ @GET("/sports") fun getSports(): Observable&
interface SportsApi {
/**
* Get the Sports from the API
*/
@GET("/sports")
fun getSports(): Observable<Sports>
}
接口API{
/**
*从API获取运动信息
*/
@获取(“/运动”)
fun getSports():可见
}
adaperclass:
class PostListAdapter: RecyclerView.Adapter<PostListAdapter.ViewHolder>() {
private lateinit var postList:Sports
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostListAdapter.ViewHolder {
val binding: ItemPostBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.context), R.layout.item_post, parent, false)
return ViewHolder(binding)
}
override fun onBindViewHolder(holder: PostListAdapter.ViewHolder, position: Int) {
holder.bind(postList)
}
override fun getItemCount(): Int {
return if(::postList.isInitialized) postList.msg.size else 0
}
fun updatePostList(postList: Sports){
this.postList = postList
notifyDataSetChanged()
}
class ViewHolder(private val binding: ItemPostBinding):RecyclerView.ViewHolder(binding.postTitle){
private val viewModel = PostViewModel()
fun bind(post: Sports){
viewModel.bind(post)
binding.viewModel = viewModel
}
}
}
class PostListAdapter:RecyclerView.Adapter(){
私有lateinit var postList:运动
重写CreateViewHolder(父级:ViewGroup,viewType:Int):PostListAdapter.ViewHolder{
val binding:ItemPostBinding=DataBindingUtil.inflate(LayoutFlater.from(parent.context),R.layout.item_post,parent,false)
返回视图保持器(绑定)
}
覆盖onBindViewHolder(holder:PostListAdapter.ViewHolder,位置:Int){
持有者绑定(postList)
}
重写getItemCount():Int{
如果(::postList.isInitialized)postList.msg.size else为0,则返回
}
乐趣更新列表(postList:Sports){
this.postList=postList
notifyDataSetChanged()
}
类ViewHolder(私有val绑定:ItemPostBinding):RecyclerView.ViewHolder(binding.postTitle){
private val viewModel=PostViewModel()
趣味绑定(帖子:体育){
viewModel.bind(post)
binding.viewModel=viewModel
}
}
}
以及PostViewModel类:
class PostViewModel:BaseViewModel() {
private val postTitle = MutableLiveData<String>()
fun bind(sports: Sports){
postTitle.value = sports.msg.toString()
}
fun getPostTitle():MutableLiveData<String>{
return postTitle
}
}
class PostListViewModel:BaseViewModel(){
@Inject
lateinit var postApi: PostApi
val postListAdapter: PostListAdapter = PostListAdapter()
val loadingVisibility: MutableLiveData<Int> = MutableLiveData()
val errorMessage:MutableLiveData<Int> = MutableLiveData()
val errorClickListener = View.OnClickListener { loadPosts() }
private lateinit var subscription: Disposable
init{
loadPosts()
}
override fun onCleared() {
super.onCleared()
subscription.dispose()
}
private fun loadPosts(){
subscription = postApi.getPosts()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe { onRetrievePostListStart() }
.doOnTerminate { onRetrievePostListFinish() }
.subscribe(
{ result -> onRetrievePostListSuccess(result) },
{ onRetrievePostListError() }
)
}
private fun onRetrievePostListStart(){
loadingVisibility.value = View.VISIBLE
errorMessage.value = null
}
private fun onRetrievePostListFinish(){
loadingVisibility.value = View.GONE
}
private fun onRetrievePostListSuccess(postList: List<String>){
postListAdapter.updatePostList(postList)
}
private fun onRetrievePostListError(){
errorMessage.value = R.string.post_error
}
}
class PostViewModel:BaseViewModel(){
private val postTitle=MutableLiveData()
趣味绑定(运动:运动){
postTitle.value=sports.msg.toString()
}
fun getPostTitle():MutableLiveData{
回帖
}
}
有人能告诉我我哪里做错了或者如何解决吗?如果你需要任何进一步的细节,请让我知道
编辑:item_post.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewModel"
type="com.gk007.example.ui.post.PostViewModel" />
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp">
<TextView
android:id="@+id/post_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textStyle="bold"
app:mutableText="@{viewModel.getPostTitle()}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</layout>
PostListViewModel类:
class PostViewModel:BaseViewModel() {
private val postTitle = MutableLiveData<String>()
fun bind(sports: Sports){
postTitle.value = sports.msg.toString()
}
fun getPostTitle():MutableLiveData<String>{
return postTitle
}
}
class PostListViewModel:BaseViewModel(){
@Inject
lateinit var postApi: PostApi
val postListAdapter: PostListAdapter = PostListAdapter()
val loadingVisibility: MutableLiveData<Int> = MutableLiveData()
val errorMessage:MutableLiveData<Int> = MutableLiveData()
val errorClickListener = View.OnClickListener { loadPosts() }
private lateinit var subscription: Disposable
init{
loadPosts()
}
override fun onCleared() {
super.onCleared()
subscription.dispose()
}
private fun loadPosts(){
subscription = postApi.getPosts()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe { onRetrievePostListStart() }
.doOnTerminate { onRetrievePostListFinish() }
.subscribe(
{ result -> onRetrievePostListSuccess(result) },
{ onRetrievePostListError() }
)
}
private fun onRetrievePostListStart(){
loadingVisibility.value = View.VISIBLE
errorMessage.value = null
}
private fun onRetrievePostListFinish(){
loadingVisibility.value = View.GONE
}
private fun onRetrievePostListSuccess(postList: List<String>){
postListAdapter.updatePostList(postList)
}
private fun onRetrievePostListError(){
errorMessage.value = R.string.post_error
}
}
class PostListViewModel:BaseViewModel(){
@注入
lateinit var postApi:postApi
val postListAdapter:postListAdapter=postListAdapter()
val loadingVisibility:MutableLiveData=MutableLiveData()
val errorMessage:MutableLiveData=MutableLiveData()
val errorClickListener=View.OnClickListener{loadPosts()}
私有lateinit var订阅:一次性
初始化{
装货柱()
}
覆盖有趣的onCleared(){
super.onCleared()
subscription.dispose()
}
私家趣味驿站(){
订阅=postApi.getPosts()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe{onRetrievePostListStart()}
.doInterminate{onRetrievePostListFinish()}
.订阅(
{result->onRetrievePostListSuccess(result)},
{onRetrievePostListError()}
)
}
private fun onRetrievePostListStart(){
loadingVisibility.value=View.VISIBLE
errorMessage.value=null
}
私人娱乐onRetrievePostListFinish(){
loadingVisibility.value=View.GONE
}
private fun onRetrievePostListSuccess(postList:List){
postListAdapter.updatePostList(postList)
}
私人娱乐onRetrievePostListError(){
errorMessage.value=R.string.post\u错误
}
}
试试这个适配器和支架。从支架上卸下视图模型。您不应该传递完整的Sports
对象,而应该只传递列表
class PostListAdapter: RecyclerView.Adapter<PostListAdapter.ViewHolder>() {
private lateinit var sports: List<String>
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostListAdapter.ViewHolder {
val binding: ItemPostBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.context), R.layout.item_post, parent, false)
return ViewHolder(binding)
}
override fun onBindViewHolder(holder: PostListAdapter.ViewHolder, position: Int) {
holder.bind(sports[position])
}
override fun getItemCount(): Int {
return if(::sports.isInitialized) sports.size else 0
}
fun updateSports(sports: List<String>){
this.sports = sports
notifyDataSetChanged()
}
class ViewHolder(private val binding: ItemPostBinding):RecyclerView.ViewHolder(binding.root){
fun bind(sport: String){
binding.postTitle.text = sport
}
}
}
class PostListAdapter:RecyclerView.Adapter(){
私有lateinit var运动:列表
重写CreateViewHolder(父级:ViewGroup,viewType:Int):PostListAdapter.ViewHolder{
val binding:ItemPostBinding=DataBindingUtil.inflate(LayoutFlater.from(parent.context),R.layout.item_post,parent,false)
返回视图保持器(绑定)
}
覆盖onBindViewHolder(holder:PostListAdapter.ViewHolder,位置:Int){
固定器绑定(运动[位置])
}
重写getItemCount():Int{
如果(::sports.isInitialized)sports.size否则返回0
}
趣味更新体育(运动:列表){
这就是运动
notifyDataSetChanged()
}
类ViewHolder(私有val绑定:ItemPostBinding):RecyclerView.ViewHolder(binding.root){
有趣的绑定(运动:字符串){
binding.postTitle.text=sport
}
}
}
然后只传递消息,列表到更新体育
并从xml中删除视图模型
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp">
<TextView
android:id="@+id/post_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</layout>
更新的视图模型:
class PostListViewModel:BaseViewModel(){
@Inject
lateinit var postApi: PostApi
val postListAdapter: PostListAdapter = PostListAdapter()
val loadingVisibility: MutableLiveData<Int> = MutableLiveData()
val errorMessage:MutableLiveData<Int> = MutableLiveData()
val errorClickListener = View.OnClickListener { loadPosts() }
private lateinit var subscription: Disposable
init{
loadPosts()
}
override fun onCleared() {
super.onCleared()
subscription.dispose()
}
private fun loadPosts(){
subscription = postApi.getPosts()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe { onRetrievePostListStart() }
.doOnTerminate { onRetrievePostListFinish() }
.subscribe(
{ result -> onRetrievePostListSuccess(result.msg) },
{ onRetrievePostListError() }
)
}
private fun onRetrievePostListStart(){
loadingVisibility.value = View.VISIBLE
errorMessage.value = null
}
private fun onRetrievePostListFinish(){
loadingVisibility.value = View.GONE
}
private fun onRetrievePostListSuccess(postList: List<String>){
postListAdapter.updateSports(postList)
}
private fun onRetrievePostListError(){
errorMessage.value = R.string.post_error
}
}
class PostListViewModel:BaseViewModel(){
@注入
lateinit var postApi:postApi
val postListAdapter:postListAdapter=postListAdapter()
val loadingVisibility:MutableLiveData=MutableLiveData()
val errorMessage:MutableLiveData=MutableLiveData()
val errorClickListener=View.OnClickListener{loadPosts()}
私有lateinit var订阅:一次性
初始化{
装货柱()
}
覆盖有趣的onCleared(){
super.onCleared()
subscription.dispose()
}
私家趣味驿站(){
订阅=postApi.getPosts()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe{onRetrievePostListStart()}
.doInterminate{onRetrievePostListFinish()}
.订阅(
{result->onRetrievePostListSuccess(result.msg)},
{onRetrievePostListError()}
)
}
private fun onRetrievePostListStart(){
loadingVisibility.value=View.VISIBLE
errorMessage.value=null
}
私人娱乐onRetrievePostListFinish(){
loadingVisibility.value=View.GONE
}
private fun onRetrievePostListSuccess(postList:List){