Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

101
Views
Cómo actualizar más de una vista remota en Android Studio Widget

Ese es mi código para el widget.

 class MovieOfTheDayWidget : AppWidgetProvider() { override fun onUpdate( context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray, ) { // There may be multiple widgets active, so update all of them for (appWidgetId in appWidgetIds) { updateAppWidget(context, appWidgetManager, appWidgetId) } } override fun onEnabled(context: Context) { // Enter relevant functionality for when the first widget is created } override fun onDisabled(context: Context) { // Enter relevant functionality for when the last widget is disabled } } internal fun updateAppWidget( context: Context, appWidgetManager: AppWidgetManager, appWidgetId: Int, ) { val randomMovieList = Constants.getAllMovies() val movie: AllMovies val randomMovieListSize = randomMovieList.size var randomPosition = Random().nextInt(randomMovieListSize) movie = randomMovieList[randomPosition] val idArray = arrayListOf<RemoteViews>() // Construct the RemoteViews object val movieName = RemoteViews(context.packageName, R.layout.movie_of_the_day_widget) movieName.setTextViewText(R.id.seriesMovieNameAndYearWidget, "${movie.movieName} (${movie.date}") val movieImg = RemoteViews(context.packageName, R.layout.movie_of_the_day_widget) movieImg.setImageViewResource(R.id.seriesMovieImgWidget, movie.moviePic) val movieRating = RemoteViews(context.packageName, R.layout.movie_of_the_day_widget) movieRating.setTextViewText(R.id.seriesMovieRatingWidget, movie.rating) idArray.add(movieName) idArray.add(movieRating) idArray.add(movieImg) // Instruct the widget manager to update the widget appWidgetManager.updateAppWidget(appWidgetId, idArray[0]) appWidgetManager.updateAppWidget(appWidgetId, idArray[1]) appWidgetManager.updateAppWidget(appWidgetId, idArray[2]) }

Pero las últimas 3 líneas no funcionan correctamente, solo funciona la segunda línea, cómo actualizar todas las RemoteViews en idArray . Intenté ponerlo en un bucle for y actualizar i , pero tampoco funcionó. ¿Es correcto llamar a la función updateAppWidget más de una vez? Intenté pasar más de un parámetro a la función pero devolvió un error.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Tiene razón, appWidgetManager.updateAppWidget() debe llamarse solo una vez.

Debe aplicar todos los cambios a la misma instancia de RemoteViews , por lo que debería ser:

 // Construct the RemoteViews object val remoteViews = RemoteViews(context.packageName, R.layout.movie_of_the_day_widget) remoteViews.setTextViewText(R.id.seriesMovieNameAndYearWidget, "${movie.movieName} (${movie.date}") remoteViews.setImageViewResource(R.id.seriesMovieImgWidget, movie.moviePic) remoteViews.setTextViewText(R.id.seriesMovieRatingWidget, movie.rating) // Instruct the widget manager to update the widget appWidgetManager.updateAppWidget(appWidgetId, remoteViews)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!