I have a problem with pass the url from fileList of urls downloaded from firebase to menu option. I need to pass specific url depending on which one was clicked to menu.
Code is working good there is no errors.
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.
Btw if somebody know how to show file name (pdf) from downloaded url I would be grateful for help
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()
}
}
}
}
CodePudding user response:
Do you really want a PopupMenu inside your list? In the list you have one item per entry in the fileList variable. So the menu would only display one option.
But your question is how to access the url in the PopupMenu onMenuItemClickListener Uri.parse()?
Anyways, the same way as you are assigning the text value of the ViewHolder TextView you can assign the url to another variable in the 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()
}
}
}
}