android - Recylerview 不显示获取的数据
问题描述
我进行 api 调用以获取列表,在 logcat 中我可以看到从 api 返回的所有组,但 Recylerview 没有显示我的项目。
视图模型
class MyGroupScreenViewMode(private val context: Context, private val codagramApi: CodagramApi) : ViewModel() {
@ExperimentalCoroutinesApi
private val myGroups = MutableLiveData<List<Group>>()
@ExperimentalCoroutinesApi
fun getMyGroups(): LiveData<List<Group>> = myGroups
init {
viewModelScope.launch(Dispatchers.IO){
val response = codagramApi.getAllGroups()
updateUi(response.groupList)
}
}
@ExperimentalCoroutinesApi
private fun updateUi(update: List<Group>){
viewModelScope.launch(Dispatchers.Main){
myGroups.value = update
}
}
和片段
class MyGroupScreen : Fragment() {
private val myGroupsAdapter: GroupAdapter by inject()
private lateinit var bindingGroup: FragmentThirdBinding
@ExperimentalCoroutinesApi
private val viewModel: MyGroupScreenViewMode by inject()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
bindingGroup = FragmentThirdBinding.inflate(layoutInflater, container, false)
return bindingGroup.root
}
@ExperimentalCoroutinesApi
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
bindingGroup.myGroups.apply {
adapter = this@MyGroupScreen.myGroupsAdapter
layoutManager = LinearLayoutManager(context)
}
myGroupsAdapter.currentList
bindingGroup.fabAdd.setOnClickListener {
it.findNavController().navigate(MyGroupScreenDirections.actionFirstViewToGroupscreen())
}
bindgetmyGroupToLiveData()
}
@ExperimentalCoroutinesApi
private fun bindgetmyGroupToLiveData() {
viewModel.getMyGroups().observe(viewLifecycleOwner, androidx.lifecycle.Observer {
myGroupsAdapter.submitList(it)
})
}
和适配器
class GroupAdapter : ListAdapter<Group,GroupScreenViewHolder>(object: DiffUtil.ItemCallback<Group>(){
override fun areItemsTheSame(oldItem: Group, newItem: Group): Boolean = oldItem.creator?.id == newItem.creator?.id
override fun areContentsTheSame(oldItem: Group, newItem: Group): Boolean =
oldItem == newItem
}) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GroupScreenViewHolder {
return GroupScreenViewHolder(
GroupscreenMygroupsBinding.inflate(LayoutInflater.from(parent.context), parent, false)
)
}
@RequiresApi(Build.VERSION_CODES.N)
override fun onBindViewHolder(holder: GroupScreenViewHolder, position: Int) {
holder.bind(getItem(position))
}
}
class GroupScreenViewHolder(private val binding: GroupscreenMygroupsBinding) :
RecyclerView.ViewHolder(binding.root) {
@RequiresApi(Build.VERSION_CODES.N)
fun bind(postData: Group) {
binding.textView.text = postData.id.toString()
}
}
解决方案
检查数据是否在列表中,然后将列表分配给适配器并调用adapter.notifyDataSetChanged(),
推荐阅读
- java - Xpath 代码在 Katalon 中有效,但在 Selenium Java 中无效
- c# - SqliteException (0x80004005): SQLite 错误 6: 数据库表被锁定 - 内存数据库具有一个写入线程和多个查询线程
- android - Xwalk 导致 NoClassDefFoundError: Failed resolution of: Ljunit/framework/Assert; 错误
- html - 如何设置博客文章片段以在其中包含可点击的 URL 链接?
- pycharm - PyCharm:使用键盘在当前插入符号上方或下方添加插入符号不起作用
- git - 如何还原与许多其他提交合并的提交,而不会丢失其间的提交(Azure Devops)
- c++ - 在 LinkedList 中插入节点会导致分段错误
- google-calendar-api - Google 日历 API(事件:列表):仅获取已接受的事件
- microsoft-graph-api - 使用 Microsoft Graph API 向频道发布消息似乎不起作用?
- amazon-web-services - 使用凭证获取:包括无法从 Chrome 隐身模式中的上一个 AWS 会话中注销