Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

104
Visualizações
How to update more than one Remote View in Android Studio Widget

That's my code for the 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])

}

But the last 3 lines don't work properly, they only the second line works, how to update all of the RemoteViews in the idArray. I've tried putting it in a for loop and updating i, but it also didn't work. Is it even the wright thing to call the updateAppWidget function more than once? I've tried passing more than one parameter to the function but it returned an error.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You are correct, appWidgetManager.updateAppWidget() should be called just once.

You need to apply all changes to the same RemoteViews instance, so it should be:

// 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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda