Как обновить текст вне addOnSuccessListener после вызова firestore ⇐ Android
-
Гость
Как обновить текст вне addOnSuccessListener после вызова firestore
I want to assign the textview (with id "check"), with the size of the document list outside the addOnSuccessListener. I want to wait for the data to store inside the list then update the textview. As further, I want to filter list with nested spinners using onItemSelectedListner. And I don't want to do it inside the addOnSuccessListener.
GalleryFragment.kt
package com.example.demo.ui.gallery import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment import androidx.lifecycle.ViewModelProvider import com.example.demo.databinding.FragmentGalleryBinding import com.google.firebase.Firebase import com.google.firebase.auth.FirebaseAuth import com.google.firebase.firestore.DocumentSnapshot import com.google.firebase.firestore.firestore class GalleryFragment : Fragment() { private var _binding: FragmentGalleryBinding? = null // This property is only valid between onCreateView and // onDestroyView. private val binding get() = _binding!! private var db = Firebase.firestore override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { val galleryViewModel = ViewModelProvider(this).get(GalleryViewModel::class.java) _binding = FragmentGalleryBinding.inflate(inflater, container, false) val root: View = binding.root val id = FirebaseAuth.getInstance().currentUser!!.uid var documents: MutableList = mutableListOf() val textV = binding.check db.collection("product").document(id).collection("products") .get() .addOnSuccessListener { result -> documents = result.documents textV.text = documents.size.toString() } return root } override fun onDestroyView() { super.onDestroyView() _binding = null } } I tried to use Kotlin Coroutines but it is showing 0 inside the textview. How can I implement it?
CoroutineScope(Dispatchers.Main).launch { try { val result = db.collection("product") .document(id) .collection("products") .get() .await() documents.addAll(result.documents) } catch (e: Exception) { Toast.makeText(requireContext(), e.localizedMessage, Toast.LENGTH_SHORT).show() } } textV.text = documents.size.toString() This is giving 0 but when putting "textV.text = documents.size.toString()" inside the coroutine it is working fine. but I want it to work outside. Is There any way to do this?
I want to assign the textview (with id "check"), with the size of the document list outside the addOnSuccessListener. I want to wait for the data to store inside the list then update the textview. As further, I want to filter list with nested spinners using onItemSelectedListner. And I don't want to do it inside the addOnSuccessListener.
GalleryFragment.kt
package com.example.demo.ui.gallery import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment import androidx.lifecycle.ViewModelProvider import com.example.demo.databinding.FragmentGalleryBinding import com.google.firebase.Firebase import com.google.firebase.auth.FirebaseAuth import com.google.firebase.firestore.DocumentSnapshot import com.google.firebase.firestore.firestore class GalleryFragment : Fragment() { private var _binding: FragmentGalleryBinding? = null // This property is only valid between onCreateView and // onDestroyView. private val binding get() = _binding!! private var db = Firebase.firestore override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { val galleryViewModel = ViewModelProvider(this).get(GalleryViewModel::class.java) _binding = FragmentGalleryBinding.inflate(inflater, container, false) val root: View = binding.root val id = FirebaseAuth.getInstance().currentUser!!.uid var documents: MutableList = mutableListOf() val textV = binding.check db.collection("product").document(id).collection("products") .get() .addOnSuccessListener { result -> documents = result.documents textV.text = documents.size.toString() } return root } override fun onDestroyView() { super.onDestroyView() _binding = null } } I tried to use Kotlin Coroutines but it is showing 0 inside the textview. How can I implement it?
CoroutineScope(Dispatchers.Main).launch { try { val result = db.collection("product") .document(id) .collection("products") .get() .await() documents.addAll(result.documents) } catch (e: Exception) { Toast.makeText(requireContext(), e.localizedMessage, Toast.LENGTH_SHORT).show() } } textV.text = documents.size.toString() This is giving 0 but when putting "textV.text = documents.size.toString()" inside the coroutine it is working fine. but I want it to work outside. Is There any way to do this?
Мобильная версия