D
D
Dewerro2021-08-19 12:58:48
Android
Dewerro, 2021-08-19 12:58:48

Removing item in RecyclerView from another Activity?

I have an activity open on click of an element from the RecyclerView at this time the RecyclerView becomes paused. After the element is removed from the activity , upon returning to the RecyclerView , it does not disappear, but after the destruction and relaunch, the element disappears. In onResume() I wrote notifyDataSetChanged () but to no avail.

adapter code:

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))
        }
    }

Also tried the solution described here . There is no result.

I also tried to add an element removal method to the Adapter, but there is no result either:
fun removeElement(element: FavQuote) {
        items.remove(element)
        notifyItemRemoved(items.indexOf(element))
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question