Skip to content Skip to sidebar Skip to footer

Json Data Not Showing In The Recylerview Mvvm

I'm trying to parse Json in Recyclerview using Data binding. When I run the app it's only showing blank screen with no error/crashes. Json data: { 'msg':[ 'football',

Solution 1:

Try with this adapter and holder. Remove ViewModel from holder. Rather than passing full Sports object you should pass only List.

classPostListAdapter: RecyclerView.Adapter<PostListAdapter.ViewHolder>() {
    privatelateinitvar sports: List<String>

    overridefunonCreateViewHolder(parent: ViewGroup, viewType: Int): PostListAdapter.ViewHolder {
        val binding: ItemPostBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.context), R.layout.item_post, parent, false)
        return ViewHolder(binding)
    }

    overridefunonBindViewHolder(holder: PostListAdapter.ViewHolder, position: Int) {
        holder.bind(sports[position])
    }

    overridefungetItemCount(): Int {

        returnif(::sports.isInitialized) sports.size else0

    }

    funupdateSports(sports: List<String>){
        this.sports = sports
        notifyDataSetChanged()
    }

    classViewHolder(privateval binding: ItemPostBinding):RecyclerView.ViewHolder(binding.root){
        funbind(sport: String){
            binding.postTitle.text = sport
        }
    }
}

Then pass only msg, List to updateSports

And also remove ViewModel from xml

<?xml version="1.0" encoding="utf-8"?><layoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"><android.support.constraint.ConstraintLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:paddingLeft="16dp"android:paddingRight="16dp"><TextViewandroid: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>

Updated ViewModel:

classPostListViewModel:BaseViewModel(){
    @Injectlateinitvar postApi: PostApi
    val postListAdapter: PostListAdapter = PostListAdapter()

    val loadingVisibility: MutableLiveData<Int> = MutableLiveData()
    val errorMessage:MutableLiveData<Int> = MutableLiveData()
    val errorClickListener = View.OnClickListener { loadPosts() }

    privatelateinitvar subscription: Disposable

    init{
        loadPosts()
    }

    overridefunonCleared() {
        super.onCleared()
        subscription.dispose()
    }

    privatefunloadPosts(){
        subscription = postApi.getPosts()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .doOnSubscribe { onRetrievePostListStart() }
            .doOnTerminate { onRetrievePostListFinish() }
            .subscribe(
                { result -> onRetrievePostListSuccess(result.msg) },
                { onRetrievePostListError() }
            )
    }

    privatefunonRetrievePostListStart(){
        loadingVisibility.value = View.VISIBLE
        errorMessage.value = null
    }

    privatefunonRetrievePostListFinish(){
        loadingVisibility.value = View.GONE
    }

    privatefunonRetrievePostListSuccess(postList: List<String>){
        postListAdapter.updateSports(postList)
    }

    privatefunonRetrievePostListError(){
        errorMessage.value = R.string.post_error
    }
}

Post a Comment for "Json Data Not Showing In The Recylerview Mvvm"