Kotlin:如何在Adapter类中调用DialogFragment?

问题描述 投票:0回答:3

我有一个MainAdapter.kt类来处理RecyclerView。在其Holder类中,我使用OnLongClickListener调用函数deleteCategory(categoryId)来删除Firebase数据库中的条目。这非常有效:

class CategoryHolder(val customView: View, var category: Category? = null) : RecyclerView.ViewHolder(customView) {
    private val TAG = CategoryHolder::class.java.simpleName

    fun bind(category: Category) {
        with(category) {
            customView.textView_name?.text = category.name
            customView.textView_description?.text = category.description

            val categoryId = category.id

            customView.setOnClickListener {
                    // do something
            }

            customView.setOnLongClickListener(
                    {
                       deleteCategory(categoryId)
                       true
                    }
            )
        }
    }

    private fun deleteCategory(categoryId: String) {
        val database = FirebaseDatabase.getInstance()

        val myRef = database.getReference("categories").child(categoryId)
        myRef.removeValue()
        Log.d(TAG, "Category with id " + categoryId + " deleted")
    }
}

但我宁愿调用DialogFragment类中的函数而不是deleteCategory(id)函数,如下所示:

  // Create an instance of a DeleteCategoryDialogFragment and show it
  fun showDeleteCategoryDialog(view: View, categoryId: String) {
      val dialog = DeleteCategoryDialogFragment.newInstance(categoryId)
      dialog.show([email protected], 
      "DeleteCategoryDialog")
  }

这给了我一个“未解决的参考:@MainActivity”错误。我怎么解决这个问题?有没有办法在我的MainActivity中获取categoryId(String类型)?这将允许我将函数showDeleteCategoryDialog移动到MainActivity并解决问题。

android firebase kotlin adapter dialogfragment
3个回答
5
投票

你不能像上面的代码那样引用你的MainActivity。在使用它之前,你必须将你的contextViewHolder作为MainActivity施放:

val activity =  itemView.context as? MainActivity
// then you can show your dialog with activity?.supportFragmentManager

0
投票

我的建议是创建一个回调此操作并在MainActivity上实现此回调,并直接在您的活动上创建对话框。

interface ClickListener {
    fun onLongClickListener(categoryId: Int)
}

然后

class CategoryHolder(val customView: View, var category: Category? = null, var mListener: ClickListener?)

...
customView.setOnLongClickListener({
      mListener?.onLongClickListener(categoryId)
      ...
}

并在您的MainActivity上:

 class MainActivity : AppCompatActivity, ClickListener {

    override fun onLongClickListener(categoryId: Int) {
     // Create your dialog
    }


    ...
        /* when creating your adapter you need to pass the listener to
         * adapter, so it can be used on your viewholder
         * mElements are the category elements, 'this' is the impl of
         * the adapter that was implemented here
        */
        mAdapter = CategoryAdapter(mElements, this)
 }

在您的适配器上:

class CategoryAdapter(var mElements: List<Category>, var mListener: ClickListener) : RecyclerView.Adapter<> ...

     // When creating your viewHolder
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryHolder {
        return CategoryHolder(LayoutInflater.from(parent.context) 
            .inflate(R.layout.category_row, parent, false), listener)
}

0
投票

这里详细说明了这是如何解决的:

  1. 在ClassHolder类中 customView.setOnLongClickListener( { showDeleteCategoryDialog(it, categoryId) true } )
  2. 在函数showDeleteCategoryDialog中 // Create an instance of a DeleteCategoryDialogFragment and show it fun showDeleteCategoryDialog(view: View, categoryId: String) { val activity = itemView.context as? MainActivity val dialog = DeleteCategoryDialogFragment.newInstance(categoryId) dialog.show(activity?.supportFragmentManager, "DeleteCategoryDialog") }
© www.soinside.com 2019 - 2024. All rights reserved.