Android - 我可以从片段访问 ViewModel 方法,但该方法不 return 列表

Android - I can access ViewModel method from fragment but the method does not return the list

我最近开始研究 android kotlin,我希望它有一个非常简单的答案,但我有一个 ViewModel,它有一个名为 getDataComment 的方法,我想从我的片段中调用该方法并提供需要论据。但是当我尝试调用它时,没有构建错误,它只是不显示列表。

我在视图模型中的方法:

fun getDataComment(postId: Int) {
        PostRepository().getDataComment(postId).enqueue(object : Callback<List<Comment>>{
            override fun onResponse(call: Call<List<Comment>>, response: Response<List<Comment>>) {
                val comments = response.body()
                comments?.let {
                    mPostsComment.value = comments!!
                }
            }

            override fun onFailure(call: Call<List<Comment>>, t: Throwable) {
                Log.e(TAG, "On Failure: ${t.message}")
                t.printStackTrace()
            }
        })
    }

片段Class

class PostDetailFragment : Fragment() {

    var param2: Int = 0
    private lateinit var binding: FragmentPostDetailBinding
    private val viewModel by viewModels<PostCommentViewModel>()

    private val gson: Gson by lazy {
        Gson()
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding =
            DataBindingUtil.inflate(inflater, R.layout.fragment_post_detail, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        checkArguments()
    }

     private fun checkArguments() {
        arguments?.let { bundle ->
            if (bundle.containsKey("POST")) {
                val postString = bundle.getString("POST", "")
                val post: Post = gson.fromJson(postString, Post::class.java)

                binding.postDetailstvTitle.text = post.title
                binding.postDetailstvBody.text = post.body
                param2 = post.id

                viewModel.getDataComment(param2)
            }
        }
    }
}

它应该是这样的:1 这是我得到的:2

如有混乱,请提前致歉,这是我第一次在这里提问,如果您需要更多信息,请告诉我。谢谢!

从视图模型

观察mPostsComment内部片段
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    checkArguments()

    viewModel.mPostsComment.observe(viewLifecycleOwner, { collectionList ->
                // do whatever you want with collectionList
    })
}