Skip to content

Instantly share code, notes, and snippets.

@Ayush783
Created July 15, 2024 12:19
Show Gist options
  • Save Ayush783/d88b00b9231e2e95b185c9d6f5baa40a to your computer and use it in GitHub Desktop.
Save Ayush783/d88b00b9231e2e95b185c9d6f5baa40a to your computer and use it in GitHub Desktop.
class ListViewRemoteViewsFactory(private val context: Context, private val intent: Intent?) : RemoteViewsService.RemoteViewsFactory{
private var articles: List<NewsArticle>? = null
private var articleImages: List<Bitmap?> = ArrayList()
override fun onCreate() {
val articlesJson: String? = intent?.extras?.getString("ARTICLES_JSON","")
if(articlesJson != null) {
...
// Load images
LoadImagesTask().execute(articles)
}
}
...
override fun getViewAt(position: Int): RemoteViews {
...
// Handle the condition for loading image
if (position < articleImages.size && articleImages[position] != null) {
views.setImageViewBitmap(R.id.header_image, articleImages[position])
} else {
views.setImageViewResource(R.id.header_image, R.drawable.ic_launcher) // Placeholder image
}
...
return views
}
...
private inner class LoadImagesTask : AsyncTask<List<NewsArticle>, Void, List<Bitmap?>>() {
...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment