使用云Firestore中的数据在Android应用程序中进行SwipeTrack初始化

使用云Firestore中的数据在Android应用程序中进行SwipeTrack初始化,android,database,kotlin,google-cloud-firestore,reactive,Android,Database,Kotlin,Google Cloud Firestore,Reactive,我是Android新手(我在Java Spring方面有一些经验),我在用从Google Firestore数据库接收的数据填充“link.fls.swipestack.swipestack”视图时遇到问题。 在从数据库接收到包含数据的列表之前,具有SwipeStack的片段将启动。因此,该列表无法及时传递给SwipeStack适配器。我如何处理这个问题 class MyViewModel(private val repository: Repository) : ViewModel() {

我是Android新手(我在Java Spring方面有一些经验),我在用从Google Firestore数据库接收的数据填充“link.fls.swipestack.swipestack”视图时遇到问题。 在从数据库接收到包含数据的列表之前,具有SwipeStack的片段将启动。因此,该列表无法及时传递给SwipeStack适配器。我如何处理这个问题

class MyViewModel(private val repository: Repository) : ViewModel() {

    var wordsListLiveData: MutableLiveData<List<WordPresentation>> = MutableLiveData()

    fun getWordsList(): LiveData<List<WordPresentation>> {
        val collectionReference = repository.getCollectionReference()

        collectionReference.get().addOnCompleteListener {
            val wordsList = it.result!!.toObjects(WordPresentation::class.java)
            wordsListLiveData.value = wordsList

        return wordsListLiveData
    }
}
类MyViewModel(私有val存储库:存储库):ViewModel(){
var-wordsListLiveData:MutableLiveData=MutableLiveData()
趣味getWordsList():LiveData{
val collectionReference=repository.getCollectionReference()
collectionReference.get().addOnCompleteListener{
val wordsList=it.result!!.toObjects(WordPresentation::class.java)
wordsListLiveData.value=wordsList
返回字ListLiveData
}
}
classmyfragment:Fragment(){
私有val viewModel by inject()
覆盖创建视图(充气机:布局充气机,容器:ViewGroup?,savedInstanceState:Bundle?):视图{
val adapter=myswipstackadapter()
adapter.setListener(此)
viewModel.getWordsList().observe(viewLifecycleOwner,Observer{
adapter.setDataList(it)
})
val绑定=碎片MyBinding.充气(充气机)
binding.lifecycleOwner=此
binding.swipeStack.adapter=适配器
binding.viewModel=viewModel
返回binding.root
}
}
类myswitstackadapter:BaseAdapter(){
私有lateinit变量wordsList:列表
覆盖视图(位置:Int,转换视图:View?,父视图:ViewGroup?):视图{
//在这里,我使用“wordsList”中的数据创建ViewHolder
}
乐趣设置数据列表(列表:列表){
wordsList=list
}
}
class MyFragment : Fragment() {

    private val viewModel by inject<MyViewModel>()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val adapter = MySwipeStackAdapter()
        adapter.setListener(this)
        viewModel.getWordsList().observe(viewLifecycleOwner, Observer {
            adapter.setDataList(it)
        })

        val binding = FragmentMyBinding.inflate(inflater)
        binding.lifecycleOwner = this
        binding.swipeStack.adapter = adapter
        binding.viewModel = viewModel
        return binding.root
    }

}
class MySwipeStackAdapter : BaseAdapter() {
   
    private lateinit var wordsList: List<WordPresentation>

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
         // here I create ViewHolders with the data from the 'wordsList'
    }

    fun setDataList(list: List<WordDePresentation>) {
        wordsList = list
    }

}