Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class UbosAdapter(val context: Context, private val ubosList: List<UboSuggestion>, val dateUtils: DateUtils) :
- RecyclerView.Adapter<RecyclerView.ViewHolder>() {
- var onClick: ((UboSuggestion, Int) -> Unit)? = null
- var onDeleteClick: ((UboSuggestion) -> Unit)? = null
- override fun getItemCount(): Int {
- return ubosList.size
- }
- override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
- (holder as? UbosViewHolder)?.bindInvoiceOnView(ubosList[position])
- }
- override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
- val layoutInflater = LayoutInflater.from(parent.context)
- return UbosViewHolder(layoutInflater.inflate(R.layout.item_ubo_layout, parent, false))
- }
- inner class UbosViewHolder(view: View) : RecyclerView.ViewHolder(view) {
- fun bindInvoiceOnView(uboSuggestion: UboSuggestion) {
- val formatedBirthDate = if (!uboSuggestion.birthDate.isNullOrEmpty()) {
- dateUtils.formatUboBirthDate(dateUtils.parseServerDate(uboSuggestion.birthDate!!))
- } else {
- ""
- }
- itemView.uboFullName.text = uboSuggestion.name
- itemView.uboBirthDate.text = formatedBirthDate
- //change to string format
- itemView.uboPercentage.text = "${uboSuggestion.percentage}%"
- itemView.setOnClickListener { onClick?.invoke(uboSuggestion, adapterPosition) }
- itemView.deleteUboButton.setOnClickListener { onDeleteClick?.invoke(uboSuggestion) }
- //Show the deletion button only if there are two or more declared UBOs
- if (ubosList.size == 1) {
- itemView.deleteUboButton.hide()
- }
- }
- }
- }
- ------------------------------------ In Activity --------------------------------------------------
- private fun setupAdapter() {
- ubosAdapter = UbosAdapter(this, ubosList, dateUtils)
- ubosRecyclerView.layoutManager = LinearLayoutManager(this)
- ubosRecyclerView.adapter = ubosAdapter
- ubosAdapter.onClick = { uboSuggestion, position ->
- selectedItemPosition = position
- navigateToNewUboDeclaration(uboSuggestion)
- }
- ubosAdapter.onDeleteClick = { uboSuggestion ->
- ubosList.remove(uboSuggestion)
- ubosAdapter.notifyDataSetChanged()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement