How to know List Size? ANDROID KOTLIN

Asked

Viewed 61 times

-1

ANDROID KOTLIN : Next: I have my Data Class with its variables,I make a call to the api and I get in my Adapter the list size to add to my attributes in xml ok.. wanted in Activity I put the amount of items I received from the api for example: if there are 3 items created in the layout show: there are 3 pending tasks and etc...

insira o código aqui

Moving class Downloadapter2 : Recyclerview.Adapter<Moving>() {

val mRetornaItens: MutableList<RetornaItensModel2> = mutableListOf()


inner class MovimentacaoVh2(itemView: View) : RecyclerView.ViewHolder(itemView) {
    fun bind(retornaItensModel2: RetornaItensModel2) {
        itemView.txt_api_numeroserie.text = retornaItensModel2.numeroSerie
        itemView.txt_api_endereço.text = retornaItensModel2.enderecoVisual
        itemView.txt_api_data.text = formatDate(retornaItensModel2.dataInclusao)
    }
}

fun formatDate(date: String): String {
    val dateTime =
        LocalDateTime.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"))
    val formatter = DateTimeFormatter.ofPattern("dd/MM/yy - HH:mm")
    val formatted = dateTime.format(formatter)
    return formatted.toString()
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MovimentacaoVh2 {
    val inflate = LayoutInflater.from(parent.context)
        .inflate(R.layout.item_retorna_itens_mov2, parent, false)
    return MovimentacaoVh2(inflate)
}

override fun onBindViewHolder(holder: MovimentacaoVh2, position: Int) {
    holder.bind(mRetornaItens[position])
}

override fun getItemCount() = mRetornaItens.size


fun Update(it: List<RetornaItensModel2>) {
    mRetornaItens.addAll(it)
    notifyDataSetChanged()
}

fun remove(position: Int) {
    mRetornaItens.removeAt(position)
    notifyItemRemoved(position)
    notifyDataSetChanged()
}

In my book I wanted to do this: val Sizelits.text = "Total pensent tasks : ${ ......}"

1 answer

0

You can simply pull the list from your Adapter for this. Ex.:

val listaTarefas = movimentacaoAdapter2.mRetornaItens
val SizeLits.text = "Total de tarefas pensentes : ${listaTarefas.size()}"

This is how I do in situations I need the Adapter list. I hope I’ve helped, see you more.

  • I’ll try very hard thank you!!!

  • I tried but always returns 0,I’m not understanding...she inflates my cardView for example I have 3 cardView,wanted to show there are 3 tasks and so on...

  • If it is returning 0, it must be because you did not initialize Adapter with the task list before calling the list to show in Textview. Make sure the list is actually filled with the items.

  • Managed to solve?

  • Oh yeah I got it straight from the body

  • Great, mark your question as resolved. Success for you!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.