Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

192
Vistas
Android val cannot be reassigned

I defined a variable called notes, when i try to modify it from a method, android studio says

val cannot be reassigned

however i can modify it if i access the variable like this: this.notes.

class NoteAdapter(var context : Context) : RecyclerView.Adapter<NoteViewHolder>(){

private var notes = emptyList<Note>()

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NoteViewHolder {
    var itemView = LayoutInflater.from(context).inflate(R.layout.note_item, parent, false)
    return NoteViewHolder(itemView)
}

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

fun setNotes(notes: List<Note>) {
    this.notes = notes
    notifyDataSetChanged()
}

Why is this happening?

I come from a Javascript background so excuse my ignorance.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You named two things notes:

private var notes = emptyList<Note>()

and:

fun setNotes(notes: List<Note>)

So, in the setNotes() function, notes refers to the function parameter. That is treated as a val and cannot be reassigned. this.notes refers to the notes property defined on your NoteAdapter class.

In general, try to use different names, to reduce confusion. For example, you might use:

fun setNotes(replacementNotes: List<Note>)
over 4 years ago · Santiago Trujillo Denunciar

0

In Kotlin,

val - When you declared any variable in Koltin as final variable so once assigned we cannot changes the value of it.

So use var when you need to define/change value while running application

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda