Skip to content Skip to sidebar Skip to footer

Blank Screen With Recyclerview No Adapter Attached

I'm tying to parse JSON in recyclerview. App compiles fine but it's outputting empty/blank screen BlogAdapter.kt class BlogAdapter(private val blogList: List) : Recycle

Solution 1:

You forget to call notifyDataSetChanged, when you setup your RecyclerView widget. Below the full method call, to make it works.

privatefunprepareRecyclerView(blogList: List<Blog>) {

    mBlogAdapter = BlogAdapter(blogList)
    if (this.resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT) {
        blogRecyclerView.layoutManager = LinearLayoutManager(this)
    } else {
        blogRecyclerView.layoutManager = GridLayoutManager(this, 4)
    }
    blogRecyclerView.itemAnimator = DefaultItemAnimator()
    blogRecyclerView.adapter = mBlogAdapter
    mBlogAdapter.notifyDataSetChanged()

}

Solution 2:

Try using below implementation:

classMainActivity : AppCompatActivity() {

    lateinitvar mainViewModel: MainViewModel
    var mBlogAdapter: BlogAdapter? = nullvar blogList: List<Blog> = arrayListOf()

    overridefunonCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        mainViewModel = ViewModelProvider(this).get(MainViewModel::class.java)

        // init your RV here 
        prepareRecyclerView()
        getPopularBlog()
        swipe_refresh.setOnRefreshListener { mainViewModel.getAllBlog() }
    }

    privatefungetPopularBlog() {
        swipe_refresh.isRefreshing = false
        mainViewModel.getAllBlog().observe(this, Observer {  charactersList ->
           blogList = charactersList
           mBlogAdapter?.notifyDataSetChanged()
        })
    }

    privatefunprepareRecyclerView() {

        mBlogAdapter = BlogAdapter(blogList)
        if (this.resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT) {
            blogRecyclerView.layoutManager = LinearLayoutManager(this)
        } else {
            blogRecyclerView.layoutManager = GridLayoutManager(this, 4)
        }
        blogRecyclerView.itemAnimator = DefaultItemAnimator()
        blogRecyclerView.adapter = mBlogAdapter

    }
}

Modify your view model like below:

classMainViewModel() : ViewModel() {

    val characterRepository= BlogRepository()

    fungetAllBlog(): MutableLiveData<List<ABCCharacters>> {
        return characterRepository.getMutableLiveData()
    }

    overridefunonCleared() {
        super.onCleared()
        characterRepository.completableJob.cancel()
    }
}

Post a Comment for "Blank Screen With Recyclerview No Adapter Attached"