I can run this and I also got data appear but I got this warning on my Run terminal "E/RecyclerView: No adapter attached; skipping layout No adapter attached; skipping layout"
How can I solve this
class SpacedFragment : Fragment() {
private lateinit var kanjiViewModel : KanjiViewModel
private lateinit var spacedRecyclerAdapter : SpacedRecyclerAdapter
private lateinit var day1Adapter: SpacedRecyclerAdapter
private var _binding : FragmentSpacedBinding? = null
private val binding get() = _binding!!
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View {
_binding = FragmentSpacedBinding.inflate(inflater,container,false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initUI()
}
private fun initUI() {
val dao = KanjiDatabase.getInstance(requireContext()).dao()
val repository = KanjiRepository(dao)
val factory = KanjiViewModelFactory(repository)
kanjiViewModel = ViewModelProvider(this,factory)[KanjiViewModel::class.java]
//adapter
spacedRecyclerAdapter = SpacedRecyclerAdapter()
day1Adapter = SpacedRecyclerAdapter()
//layoutManager
binding.spacedRecyclerview.layoutManager = GridLayoutManager(context,5)
binding.spacedRecyclerviewDay1.layoutManager = GridLayoutManager(context,5)
getData()
}
private fun getData() {
kanjiViewModel.kanjiList.observe(viewLifecycleOwner, { data ->
spacedRecyclerAdapter.submitList(data.filter { it.spacedstatus == 0 })
binding.spacedRecyclerview.adapter = spacedRecyclerAdapter
day1Adapter.submitList(data.filter { it.Japanese_Language_Proficiency_Test == 4 })
binding.spacedRecyclerviewDay1.adapter = day1Adapter
})
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
CodePudding user response:
There is a sorting error in the relevant lines.
day1Adapter.submitList(data.filter { it.Japanese_Language_Proficiency_Test == 4 })
binding.spacedRecyclerviewDay1.adapter = day1Adapter
You should use submitlist after assigning adapter
CodePudding user response:
set your adapter inside initUI
function
private fun initUI() {
...code
spacedRecyclerAdapter = SpacedRecyclerAdapter()
binding.spacedRecyclerview.adapter = spacedRecyclerAdapter
day1Adapter = SpacedRecyclerAdapter()
binding.spacedRecyclerviewDay1.adapter = day1Adapter
...code
getData()
}
private fun getData() {
kanjiViewModel.kanjiList.observe(viewLifecycleOwner, { data ->
spacedRecyclerAdapter.submitList(data.filter { it.spacedstatus == 0 })
day1Adapter.submitList(data.filter { it.Japanese_Language_Proficiency_Test == 4 })
})
}