Updating ArticleAdapter

Before we can continue, we will need to update the logic that we implemented in ArticleAdapter. What we want to do is remove the code related to the ArticleLoader, since this implementation will not load on demand. First, let's update the constructor so that it doesn't take an ArticleAdapter:

class ArticleAdapter
: RecyclerView.Adapter<ArticleAdapter.ViewHolder>() {

...
}

Now we can update the function onBindViewHolder() so that it doesn't try to load more articles when needed. The updated implementation looks as follows:

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val article = articles[position]

holder.feed.text = article.feed
holder.title.text = article.title
holder.summary.text = article.summary
}

We will add a function to add articles incrementally to the adapter. Let's add this function at the end of the class:

fun add(article: Article) {
this.articles.add(article)
notifyDataSetChanged()
}

Below it, let's add a function to clear the adapter. This will come in handy because we need to clear the list between searches:

fun clear() {
this.articles.clear()
notifyDataSetChanged()
}

You will need to update MainActivity so that it doesn't pass in this when instantiating the adapter. After that, you will be able to use this adapter for the search feature.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.254.61