0

so, I heard there are no ternary operators in kotlin, so, how can I achieve the following in kotlin data class, but i want only a part of the data from the data class to show in the recycler view based on a certain condition

below shows how it is done in JS

 {online.map(function(item){
     
         return (
          <div key={item.uid} >

  {item.Gender== 'Male' ?
          
          <li >

<div><img src={item.photoURL}/><p><b>{item.displayName}</b></p></div>
    </li>
         
: null}
                   
          </div>
    )
    })}

online is an object that contains the list of user information {item.Gender === 'Male' ? checks and returns only users that are male

so, can that similar feature be achieved in Kotlin?

Kotlin data class

data class UserInfo(
    val uid: Uids) {
        data class Uids (
    var Gender: String? = null,
    var uid: String? = null,
    var displayName: String? = null,
    var photoURL: String? = null)
}
class BalAdapter(val context: Context, private val dataList: ArrayList<UserInfo.Uids>, private val layout: Int) :
    RecyclerView.Adapter<BalAdapter.Holder>() {
    lateinit var listener: (UserInfo.Uids) -> Unit
    override fun onBindViewHolder(holder: Holder, position: Int) {
        val item = dataList[position]
        holder.bindItems(item)
        holder.itemView.setOnClickListener { listener(item) }

    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
        val view = LayoutInflater.from(parent.context).inflate(layout, parent, false)
        return Holder(view)

    }

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


    inner class Holder(view: View?) : RecyclerView.ViewHolder(view!!) {
        val recordCategory = view?.findViewById<ImageView>(R.id.itemImage)
        val recordNote = view?.findViewById<TextView>(R.id.firstname)
        val recordAmount = view?.findViewById<TextView>(R.id.lastname)

        fun bindItems(bal: UserInfo.Uids) {
            Picasso.get()
                .load(bal.photoURL)
                .resize(100, 100)
                .centerCrop()
                .into(recordCategory)
            recordNote?.text = bal.displayName
            recordAmount?.text = bal.email
            itemView.setOnClickListener {
                listener.invoke(UserInfo.Uids())

                //  recordDeleteImageView.imageb
            }
        }
    }
}

would the conditions be placed in the adapter, in the fragment class containing the recyclerView, or in the class that is populating the data class? and how would it be done?

Raphael Inyang
  • 173
  • 1
  • 4
  • 13

1 Answers1

1

Just create another method to update your value in adapter

class BalAdapter(val context: Context, private val dataList: ArrayList<UserInfo.Uids>, private val layout: Int) :
    RecyclerView.Adapter<BalAdapter.Holder>() {

    internal fun updateValue(userInfo: UserInfo) {
        dataList = userInfo
        notifyDataSetChanged()
    }

}

Then, in your view, just call this function with latest value

val result  = UserInfo.map { it.Gender == "Male" }

balAdapter.updateValue(result)
Teo
  • 876
  • 9
  • 22