Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

350
Vistas
ArrayList está vacío fuera del ámbito Coroutine

Estoy construyendo un proyecto basado en Android MVVM Architecture. Escribí mi propia API para obtener el Ddta. La API devuelve una ArrayList de telas. Uso Retrofit para llamar a la API. La clase Repository funciona bien y devuelve la lista de la API, pero el problema ocurrió en ViewModel.

Cuando accedo a ArrayList dentro de una rutina , funciona bien (usando coroutine porque getClothList es una función Suspender dentro de la clase Repository), pero no puedo acceder a ArrayList fuera del alcance de coroutine.

la lista de ropa está vacía fuera de viewModelScope

Ver modelo

 class ClothViewModel(private val repository: ClothRepository) :ViewModel() { lateinit var clothList:ArrayList<ClothModel> init { viewModelScope.launch(Dispatchers.IO) { clothList = repository.getClothsList() Log.d("ClothList ->",clothList.toString()) } } fun getData():ArrayList<ClothModel>{ clothList = repository.clothsList Log.d("View Model ->",clothList.toString()) return clothList } }

Clase de repositorio

 class ClothRepository(private val clothService: ClothService) { val clothsList = ArrayList<ClothModel>() suspend fun getClothsList():ArrayList<ClothModel>{ val result = clothService.getClothList() if (result?.body() != null) { clothsList.add(result.body()!!) } Log.d("Result ->",result.body().toString()) return clothsList } }

Actividad principal

Como no obtengo la Lista en viewModel, no puedo usarla dentro de mainActivity

 class WelcomeScreen: AppCompatActivity() { var arrayList = ArrayList<ClothModel>() var arrayList1 = ArrayList<ClothModel>() lateinit var viewModel :ClothViewModel var state =1 lateinit var firebaseAuth:FirebaseAuth override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.dashboard) val clothService = ClothHelper.getInstance().create(ClothService::class.java) val clothRepository = ClothRepository(clothService) viewModel = ViewModelProvider(this,ClothViewModelFactory(clothRepository)).get(ClothViewModel::class.java) arrayList = viewModel.getData() //calling the viewModel function setupADapter() } fun setupADapter(){ var list = listOf("Nike","Romans","CK","Jordan","Kizumik","Hishida") var recyclerView = findViewById<RecyclerView>(R.id.cloths) Log.d("Passing ->",arrayList1.toString()) recyclerView.adapter = ClothViewAdapter(arrayList1) recyclerView.layoutManager = GridLayoutManager(applicationContext,2) } }
over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda