RecyclerView, No adapter attached : skipping layout

60 Views Asked by At

I'm creating a main page display in the form of a list of stories. I retrieve the token from the API after a successful login. The token was successfully retrieved but the list story did not appear because of the error

no adapter attached: skipping layout.


@Suppress("DEPRECATION")
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {

    companion object{
        const val EXTRA_TOKEN = "extra_token"
    }
    private var binding: ActivityMainBinding? = null
    private var token: String = " "

    private lateinit var recyclerView: RecyclerView
    private lateinit var listAdapter: StoryAdapter
    private val mainViewModel: MainViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding?.root)

        token = intent.getStringExtra(EXTRA_TOKEN).toString()


// Didn't I define the recyclerView first? Why is it still an error?

        listAdapter = StoryAdapter()
        recyclerView = binding?.rvStory!!
        recyclerView.adapter = listAdapter

        lifecycleScope.launchWhenCreated {
            launch {
                mainViewModel.getAuthToken().collect {itToken ->
                    if (!itToken.isNullOrEmpty())
                        token = itToken
                    getAllStories(token)
                    setSwipeLayout()
                }
            }
        }

        binding?.fabAddStory?.setOnClickListener{
            Intent(this, AddStoryActivity::class.java).also { intent ->
                startActivity(intent)
            }
        }

    }

    private fun setSwipeLayout(){
        binding?.srLayout?.setOnRefreshListener {
            getAllStories(token)
            binding!!.srLayout.isRefreshing = false
        }
    }

    private fun getAllStories(token:String){
        lifecycleScope.launch {
            lifecycle.repeatOnLifecycle(Lifecycle.State.RESUMED) {
                mainViewModel.getAllStories(token).collect{ result ->
                    result.onSuccess {
                        val response = result.getOrNull()
                        val liststory = response?.listStory as List<ListStoryItem>
                        Log.d("MainActivity", "List Story Size: ${liststory.size}")

                        setupRecyclerView()
                        setUpadateStories(liststory)
                    }
                    result.onFailure {
                        val response = result.exceptionOrNull()
                        response?.printStackTrace()
                    }
                }
            }
        }
    }

    private fun setUpadateStories(liststory: List<ListStoryItem>){
        val recyclerViewState = recyclerView.layoutManager?.onSaveInstanceState()
        listAdapter.submitList(liststory)
        recyclerView.layoutManager?.onRestoreInstanceState(recyclerViewState)
    }

    private fun setupRecyclerView() {
        recyclerView = binding?.rvStory!!
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = listAdapter
    }
}

Image of error

1

There are 1 best solutions below

0
sanitizedUser On

In the method onCreate you are not attaching a LayoutManager.

The simplest solution is to replace these lines in the method onCreate:

recyclerView = binding?.rvStory!!
recyclerView.adapter = listAdapter

With this:

setupRecyclerView()

Also you could change the definition of the method setupRecyclerView to better reflect what orientation of the LayoutManager you want:

private fun setupRecyclerView() {
    recyclerView = binding?.rvStory!!
    recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
    recyclerView.adapter = listAdapter
}