首页 > 解决方案 > 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()


        }
    }

标签: androidkotlinandroid-recyclerview

解决方案


检查数据是否在列表中,然后将列表分配给适配器并调用adapter.notifyDataSetChanged(),


推荐阅读