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

时间:2018-04-10 14:20:35

标签: android firebase kotlin adapter dialogfragment

我有一个处理RecyclerView的MainAdapter.kt类。在其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(this@MainActivity.supportFragmentManager, 
      "DeleteCategoryDialog")
  }

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

3 个答案:

答案 0 :(得分:5)

您无法像上面的代码那样引用您的MainActivity。在使用之前,您必须将context的{​​{1}}投射为ViewHolder

MainActivity

答案 1 :(得分: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)
}

答案 2 :(得分: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")
    }