将 url(从 firebase)传递给 ViewHolder 适配器中的菜单选项的 KOTLIN 问题

KOTLIN problem with passing url (from firebase) to menu option in ViewHolder adapter

我无法将 url from fileList of url 从 firebase 下载到菜单选项。我需要传递特定的 url,具体取决于单击哪个菜单。

代码运行良好,没有错误。

Here is my code of adapter where is the problem

class AdapterKarty(private val fileList: List<plik>, private val context: Context): RecyclerView.Adapter<AdapterKarty.ViewHolder>(){


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AdapterKarty.ViewHolder {
        val a = LayoutInflater.from(parent.context).inflate(R.layout.karta_pliku,parent, false)

        return ViewHolder(a);

    }

    override fun onBindViewHolder(holder: AdapterKarty.ViewHolder, position: Int) {

Here i chose specific url for ViewHolder cards but i need something like this below.

顺便说一句,如果有人知道如何显示下载的文件名 (pdf) url 我将不胜感激

           var item = fileList[position]

            holder.dokument.text = item.url


    }

    override fun getItemCount(): Int {
        return fileList.size
    }


    inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {

        var dokument: TextView = itemView.findViewById(R.id.tv_dokument)

        private var menu: ImageView = itemView.findViewById(R.id.iv_menu)



        init {
            menu.setOnClickListener {
                val popupMenu = PopupMenu(itemView.context, it)
                popupMenu.setOnMenuItemClickListener { item ->
                    when (item.itemId) {
                        R.id.menu_open -> {

Here i need open specific url depending on chosen option. I need put something like above (item.url) to Uri.parse. Maybe im wrong but I dont know. Static url is working but i must get specific from fileList.

                            val intent = Intent(Intent.ACTION_VIEW, Uri.parse())
                            context.startActivity(intent)
                            true
                        }
                        else -> false
                    }


                }
                popupMenu.inflate(R.menu.main_menu)
                popupMenu.show()
            }


        }
    }


}

您真的想要列表中的 PopupMenu 吗?在列表中,fileList 变量中的每个条目都有一个项目。所以菜单只会显示一个选项。

但是你的问题是如何访问 PopupMenu onMenuItemClickListener 中的 url Uri.parse()?

无论如何,就像您分配 ViewHolder TextView 的文本值一样,您可以将 url 分配给 ViewHolder 中的另一个变量。

class AdapterKarty(private val fileList: List<plik>, private val context: Context): RecyclerView.Adapter<AdapterKarty.ViewHolder>(){

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AdapterKarty.ViewHolder {
    val a = LayoutInflater.from(parent.context).inflate(R.layout.karta_pliku,parent, false)
    return ViewHolder(a);
}

override fun onBindViewHolder(holder: AdapterKarty.ViewHolder, position: Int) {
    var item = fileList[position]
--> holder.filePath = item.url
    holder.dokument.text = item.url
}

override fun getItemCount(): Int {
    return fileList.size
}

inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
--> var filePath: String
    var dokument: TextView = itemView.findViewById(R.id.tv_dokument)
    private var menu: ImageView = itemView.findViewById(R.id.iv_menu)

    init {
        menu.setOnClickListener {
            val popupMenu = PopupMenu(itemView.context, it)
            popupMenu.setOnMenuItemClickListener { item ->
                when (item.itemId) {
                    R.id.menu_open -> {
                        val intent = Intent(Intent.ACTION_VIEW, Uri.parse(filePath)) <---
                        context.startActivity(intent)
                        true
                    }
                    else -> false
                }
            }
            popupMenu.inflate(R.menu.main_menu)
            popupMenu.show()
        }
    }
}

}