Skip to content

Instantly share code, notes, and snippets.

@Ayush783
Created July 15, 2024 08:17
Show Gist options
  • Save Ayush783/9b646d0e74e73666aa3eaeb9ab55cf63 to your computer and use it in GitHub Desktop.
Save Ayush783/9b646d0e74e73666aa3eaeb9ab55cf63 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
override fun onCreate() {
val articlesJson: String? = intent?.extras?.getString("ARTICLES_JSON","")
if(articlesJson != null) {
val articleListType = object : TypeToken<List<NewsArticle>>() {}.type
val data: List<NewsArticle> = Gson().fromJson(articlesJson, articleListType)
articles = data
}
}
override fun getCount(): Int {
return articles!!.size
}
override fun getViewAt(position: Int): RemoteViews {
val article: NewsArticle = articles!![position]
val views = RemoteViews(context.packageName, R.layout.list_tile)
views.setTextViewText(R.id.title, article.title)
views.setImageViewResource(R.id.header_image, R.drawable.ic_launcher)
val fillInIntent = Intent()
fillInIntent.data = Uri.parse("package://<Add required data you need to pass>")
views.setOnClickFillInIntent(R.id.list_tile, fillInIntent)
return views
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment