Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. class UbosAdapter(val context: Context, private val ubosList: List<UboSuggestion>, val dateUtils: DateUtils) :
  2. RecyclerView.Adapter<RecyclerView.ViewHolder>() {
  3.  
  4. var onClick: ((UboSuggestion, Int) -> Unit)? = null
  5.  
  6. var onDeleteClick: ((UboSuggestion) -> Unit)? = null
  7.  
  8. override fun getItemCount(): Int {
  9. return ubosList.size
  10. }
  11.  
  12. override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
  13. (holder as? UbosViewHolder)?.bindInvoiceOnView(ubosList[position])
  14. }
  15.  
  16. override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
  17. val layoutInflater = LayoutInflater.from(parent.context)
  18. return UbosViewHolder(layoutInflater.inflate(R.layout.item_ubo_layout, parent, false))
  19. }
  20.  
  21. inner class UbosViewHolder(view: View) : RecyclerView.ViewHolder(view) {
  22. fun bindInvoiceOnView(uboSuggestion: UboSuggestion) {
  23. val formatedBirthDate = if (!uboSuggestion.birthDate.isNullOrEmpty()) {
  24. dateUtils.formatUboBirthDate(dateUtils.parseServerDate(uboSuggestion.birthDate!!))
  25. } else {
  26. ""
  27. }
  28. itemView.uboFullName.text = uboSuggestion.name
  29. itemView.uboBirthDate.text = formatedBirthDate
  30. //change to string format
  31. itemView.uboPercentage.text = "${uboSuggestion.percentage}%"
  32. itemView.setOnClickListener { onClick?.invoke(uboSuggestion, adapterPosition) }
  33. itemView.deleteUboButton.setOnClickListener { onDeleteClick?.invoke(uboSuggestion) }
  34.  
  35. //Show the deletion button only if there are two or more declared UBOs
  36. if (ubosList.size == 1) {
  37. itemView.deleteUboButton.hide()
  38. }
  39. }
  40. }
  41. }
  42. ------------------------------------ In Activity --------------------------------------------------
  43.  
  44. private fun setupAdapter() {
  45. ubosAdapter = UbosAdapter(this, ubosList, dateUtils)
  46. ubosRecyclerView.layoutManager = LinearLayoutManager(this)
  47. ubosRecyclerView.adapter = ubosAdapter
  48. ubosAdapter.onClick = { uboSuggestion, position ->
  49. selectedItemPosition = position
  50. navigateToNewUboDeclaration(uboSuggestion)
  51. }
  52. ubosAdapter.onDeleteClick = { uboSuggestion ->
  53. ubosList.remove(uboSuggestion)
  54. ubosAdapter.notifyDataSetChanged()
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement