vad070
vad070

Reputation: 37

How to pass data from RecyclerView to DialogFragment in Kotlin?

My RecyclerView has a list of items, each item has it's clickable "delete icon", which removes item from the list. How can I use DialogFragment for confirmation of removal?

Here is a code for FeedAdapter (my RecyclerView adapter):

class FeedAdapter(private val posts: List<FeedPost>) : RecyclerView.Adapter<FeedAdapter.ViewHolder>() { 
        private val listener: (() -> Unit)? = null

fun setListener(listener: (() -> Unit)?) {
        this.listener = listener
    }

class ViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
val view = LayoutInflater.from(parent.context).inflate(R.layout.adapt_item, parent, false)
        return ViewHolder(view)
    }

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val post = posts[position]
with(holder.view) {
            user_photo_image.loadUserPhoto(posts[position].photo)
            date_text.text = post.kogda
            caption_text.setCaptionText(nachalo, post.caption)
            test.text = post.ur
            delete_image.setOnClickListener { listener?.invoke()  }
           }
}

override fun getItemCount() = posts.size

private fun ImageView.loadUserPhoto(photoUrl: String?) {
            if (!(context as Activity).isDestroyed) {
                GlideApp.with(this).load(photoUrl).fallback(R.drawable.person).into(this)
            }
        }
    }

So, when you click on delete_image, DeleteDialog appears with 2 buttons - "OK" and "CANCEL". How to make a confirmation with a click on "OK" button and delete item from a list?

Here is a code for my DeleteDialog:

class DeleteDialog : DialogFragment() {
override fun onAttach(context: Context) {
        super.onAttach(context)}

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val view = activity!!.layoutInflater.inflate(R.layout.dialog_delete, null)
return AlertDialog.Builder(context).setView(view)
    .setPositiveButton(android.R.string.ok, {_, _ -> })
    .setNegativeButton(android.R.string.cancel, {_, _ -> })
    .setTitle("Are you sure you want to delete?").create()
    }
}

I tried to use this:

.setPositiveButton(android.R.string.ok, {_, _ ->
     mDatabase.child("feed-posts").child("${post.ur}").removeValue()

but unfortunately it can't recognize "post.ur" reference, which I tried to pass from ViewHolder and which stands for child path for certain item. Also I tried to find another variants for realization in Net, but it wasn't successful.

Upvotes: 0

Views: 534

Answers (1)

Edson
Edson

Reputation: 51

Try to send the object post back with a interface, create a interface

interface ClickPost {
fun postClicked(mPost: Post)}

and on your adapter you implement in a variable

class FeedAdapter(private val posts: List<FeedPost>, val click: ClickPost)

on click inside onBind:

delete_image.setOnClickListener { click.postClicked(post)  }

like that you can get it back in the fragment/activity that contains this adapter, them you can just send the post via bundle or parameter to the dialogFragment

Upvotes: 1

Related Questions