Created
July 15, 2024 12:19
-
-
Save Ayush783/d88b00b9231e2e95b185c9d6f5baa40a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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