Удаление элемента в RecyclerView из другого Activity
У меня открывается activity по клику элемента из RecyclerView в это время RecyclerView становится на паузу. После удаления элемента из activity по возвращению в RecyclerView он не пропадает, но после уничтожения и нового запуска элемент исчезает. В onResume() прописывал notifyDataSetChanged() но безрезультатно.
Код adapter'a:
var items: ArrayList<FavQuote> = ArrayList()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return FavQuoteViewHolder(
LayoutInflater.from(parent.context).inflate(R.layout.list_item_favs, parent, false)
)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (holder) {
is FavQuoteViewHolder -> {
holder.bind(items[position])
}
}
}
override fun getItemCount(): Int {
return items.size
}
class FavQuoteViewHolder
constructor(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val quote: TextView = itemView.quote
private val quoteAuthor: TextView = itemView.quote_author
private val selectQuote: Button = itemView.select_quote
private val mActivity = itemView.context as Activity
private val intent = Intent(mActivity, QuoteSelected::class.java)
fun bind(favQuote: FavQuote) {
quote.text = favQuote.quote
quoteAuthor.text = favQuote.author
selectQuote.setOnClickListener {
intent.putExtra("quote", favQuote.quote)
intent.putExtra("author", favQuote.author)
intent.putExtra("id", favQuote.id)
mActivity.startActivity(intent)
mActivity.overridePendingTransition(R.anim.fade_in, R.anim.fade_out)
}
}
}
fun submitQuoteInfo(quote: String, author: String, id: Int) {
val element = FavQuote(quote, author, id)
if (items.contains(element)) {
return
} else {
items.add(element)
notifyItemInserted(items.indexOf(element))
}
}
Так же пробовал решение, описанное здесь. Результата нет.
Так же попробовал добавить метод удаления элемента в Adapter'e, но результата так же нет:
fun removeElement(element: FavQuote) {
items.remove(element)
notifyItemRemoved(items.indexOf(element))
}