I know to capitalize the arraylist with strings as data can be done with
list.map({ it.capitalize()})
which returns as a list.
Now, what if it's a data class instead of strings?
class MainActivity : AppCompatActivity() {
val animals: ArrayList<Animals> = ArrayList()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
addAnimals()
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.addItemDecoration(DividerItemDecoration(this,DividerItemDecoration.VERTICAL))
recyclerView.adapter = AnimalAdapter(this, animals)
}
data class Animals(val name: String, val type: String)
fun addAnimals() {
animals.add(Animals("dog","bark"))
animals.add(Animals("cat","meow"))
animals.add(Animals("owl","hoot"))
animals.add(Animals("cheetah","roar, growl, snarl"))
animals.add(Animals("raccoon","trill"))
animals.add(Animals("bird","chirp"))
animals.add(Animals("snake","hiss"))
animals.add(Animals("lizard","?"))
animals.add(Animals("hamster","squeak"))
animals.add(Animals("bear","roar, growl"))
animals.add(Animals("lion","roar, growl, snarl"))
animals.add(Animals("tiger","roar, growl, snarl"))
animals.add(Animals("horse","neigh"))
animals.add(Animals("frog","croak"))
animals.add(Animals("fish","?"))
animals.add(Animals("shark","?"))
animals.add(Animals("turtle","?"))
animals.add(Animals("elephant","trumpet"))
animals.add(Animals("cow","moo"))
animals.add(Animals("beaver","?"))
animals.add(Animals("bison","moo"))
animals.add(Animals("porcupine","?"))
animals.add(Animals("rat","woof"))
animals.add(Animals("mouse","squeak"))
animals.add(Animals("goose","honk, hiss"))
animals.add(Animals("deer","bellow"))
animals.add(Animals("fox","bark, howl, growl, bay"))
animals.add(Animals("moose","bellow"))
animals.add(Animals("buffalo","moo"))
animals.add(Animals("monkey","scream, chatter"))
animals.add(Animals("penguin","?"))
animals.add(Animals("parrot","squawk"))
}
AnimalAdapter:
private class AnimalAdapter(val context: Context, val items: ArrayList<Animals>) : RecyclerView.Adapter<ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(LayoutInflater.from(context).inflate(R.layout.animal_item, parent, false))
}
override fun getItemCount(): Int {
return items.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.tvAnimalType?.text = items.get(position).name.capitalize()
holder.tvAnimalSounds?.text = items.get(position).type.capitalize()
holder.itemView.setOnClickListener {
val alertDialog: AlertDialog.Builder = AlertDialog.Builder(context)
alertDialog.setMessage("Success")
.setPositiveButton("Ok") { dialog, which -> dialog.dismiss()
}
.setNegativeButton("Cancel") { dialog, which -> dialog.dismiss()
}
val alert = alertDialog.create()
// set title for alert dialog box
alert.setTitle("AlertDialogExample")
// show alert dialog
alert.show()
}
}
}
private class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
// Holds the TextView that will add each animal to
val tvAnimalType = view.animal_type
val tvAnimalSounds = view.animal_sounds
}
P.S : I know that I can capitalize it in the adapter class while setting it, which I have already done. But what if I have to do that before passing the List to the adapter?