Adapter group by name resulting unnecessry group header

18 Views Asked by At

I'm trying to make a list that are grouped by it's first name for example

  1. Hello
  2. Hotel
  3. Kargo
  4. Kali

and it will result

  • H
  • Hello
  • Hotel
  • K
  • Kargo
  • Kali

I have successfully created the adapter with the result I want but for some reason, unnecessary header is also shown

Incorrectheader

as you can see from the picture above, I don't have a list with letters I or J but for some reason, it still shows up as a group header

how to fix it?

here is the code I use

  • Models.kt

    data class SiteModel (
    
      val IDSite: Int,
      val IDPerusahaan:Int? = null,
      val KodeSite:String? = null,
      val NamaSite:String? = null,
      val Alamat:String? = null,
      val Telepon:String? = null,
      val Telepon2:String? = null,
      val JumlahShift:Int? = null,
      val TanggalMulaiBergabung:Timestamp? = null,
      val TanggalSelesaiBergabung:Timestamp? = null,
      val Catatan:String? = null,
      val StatusSite:Int? = null,
      val IDSiteSelesai: Int? = null,
      val QRCodeImage:String? = null,
      val CreateBy:String? = null,
      val CreateAt:Timestamp? = null,
      val LastUpdateBy:String? = null,
      val LastUpdateAt:Timestamp? = null
    
    )
    
  • SiteAdapter.kt

    class SiteAdapter(
      private val myActivity: Activity,
      private val myList: List<Models.SiteModel>
    ) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    
      private val VIEW_TYPE_GROUP = 0
      private val VIEW_TYPE_ITEM = 1
    
      inner class GroupViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
          val groupTextView: TextView = itemView.findViewById(android.R.id.text1)
      }
    
      inner class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
          val siteTextView: TextView = itemView.findViewById(android.R.id.text1)
      }
    
    
      override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
          return if (viewType == VIEW_TYPE_GROUP) {
              val view = LayoutInflater.from(parent.context).inflate(android.R.layout.simple_list_item_1, parent, false)
              GroupViewHolder(view)
          } else {
              val view = LayoutInflater.from(parent.context).inflate(android.R.layout.simple_list_item_1, parent, false)
              ItemViewHolder(view)
          }
      }
    
      override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
          val item = myList[position]
          if (getItemViewType(position) == VIEW_TYPE_GROUP) {
              val groupHolder = holder as GroupViewHolder
              // Set huruf pertama di sini
              groupHolder.groupTextView.text = item.NamaSite!!.substring(0, 1).uppercase()
    
              groupHolder.groupTextView.setBackgroundResource(R.color.primary)
              groupHolder.groupTextView.setTextColor(ContextCompat.getColor(myActivity, R.color.onPrimary))
          } else {
              val itemHolder = holder as ItemViewHolder
              // Set nama site di sini
              itemHolder.siteTextView.text = item.NamaSite!! + " - " + item.KodeSite!!
          }
      }
    
      override fun getItemViewType(position: Int): Int {
          // Cek apakah posisi saat ini merupakan awal dari grup
          val namaSite = myList[position].NamaSite!!.uppercase()
          return if (position == 0 || namaSite[0] != myList[position - 1].NamaSite!!.uppercase()[0]) {
              VIEW_TYPE_GROUP
          } else {
              VIEW_TYPE_ITEM
          }
      }
    
      override fun getItemCount(): Int {
          return myList.size
      }
    }
    
0

There are 0 best solutions below