I'm trying to work with group By and I can't, my intention is to group the companies within the *poblacion. How can I group so that within poblacion it shows me the companies? it only shows me a repeated item
val works is the list that I receive from the fragment, and what I want is that in the same CardView it shows me the companies that have worked in said population
Poblacion Naquera and within that show me company1 and company2
I hope I explained myself better
This is my Json
{
"resultsObras": [
{
"id": "2",
"empresa": "company 1",
"latitude": "xxx",
"longitude": "xx",
"poblacion": "Naquera",
"direccion": "Caseta",
"obra": ""
},
{
"id": "3",
"empresa": "company 2",
"latitude": "xxx",
"longitude": "xxx",
"poblacion": "Naquera",
"direccion": "Pl. Padre Manuel Navarro",
"obra": "Colector"
}
]
}
and this is the adapter
class ObrasAdapter(val obras : List<ResultsObras>) :
RecyclerView.Adapter<ObrasAdapter.ViewHolder>() {
obras
private lateinit var mContext: Context
override fun onCreateViewHolder(parent : ViewGroup, viewType : Int) : ViewHolder {
mContext = parent.context
val layoutInflater = LayoutInflater.from(parent.context)
return ViewHolder(layoutInflater.inflate(R.layout.obras_item, parent, false))
}
override fun onBindViewHolder(holder : ViewHolder, position : Int) {
val groupCity = obras
.groupingBy(ResultsObras::empresa)
.reduce { _, groupState, employee ->
if (groupState.empresa.isEmpty()) groupState else employee
}
groupCity.forEach { (company, state) ->
holder.itemView.tvPoblacion.text = "${state.poblacion} $company"
}
val item = obras[position]
holder.bind(item)
holder.itemView.setOnClickListener { view ->
val intent = Intent(view.context, MapsActivity::class.java)
intent.putExtras(Bundle().apply {
putParcelable("OBRAS", item)
})
view.context.startActivity(intent)
}
}
override fun getItemCount() : Int {
return obras.size
}
class ViewHolder(view : View) : RecyclerView.ViewHolder(view) {
private val binding = ObrasItemBinding.bind(view)
fun bind(clientes : ResultsObras) {
//println("OBRAS conjunto1 " + conjunto1)
// binding.tvPoblacion.text = clientes.toString()
// binding.tvEmpresa.text = clientes.empresa
// binding.tvObra.text = clientes.direccion
// binding.tvCometario.text = clientes.obra
}
}
}