In my code i want to execute holder.task.setChecked() command with item.getStatus()) as a parameter.
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = data[position]
holder.task.text = item.getTask()
holder.task.setChecked(item.getStatus())
}
The problem is that setChecked requires type Boolean, but from getStatus function i get Boolean.Companion in return.
class Model(itemView: View) : RecyclerView.ViewHolder(itemView) {
private var id = Int
private var status = Boolean
private var task = String
fun getStatus(): Boolean.Companion {
return status
}
When i try to change the return type of this function, i get thrown with an error.
Been thinking for a while about how to change the types to be equal, but i cannot seem to find the solution.
To be clear, setChecked function is a default function which im executing on Checkbox object
2
Answers
The
setChecked
function requires aBoolean
param, but the return type ofgetStatus
isBoolean.Companion
. This is because you have defined thestatus
field as ofBoolean.Companion
type.To fix this, you may define
status
as a normal class level field and either assign a default value, or provide a constructor to initialize the value appropriately.Then you can change the
getStatus
function to return aBoolean
type instead ofBoolean.Companion
and then use it to call
holder.task.setChecked(item.getStatus())
.Your example code is very unusual, and I’m not sure you actually meant to do what you’re doing here.
This for example
doesn’t hold an
Int
.id
is not a number. It’s actually the companion object for theInt
class, which in this case holds the constantsMAX_VALUE
,MIN_VALUE
,SIZE_BYTES
andSIZE_BITS
. Usually you’d access these directly on theInt
class, likeInt.MAX_VALUE
– when you do that you’re actually accessing them on the companion object, which is why assigning the valueInt
to a variable gives you that companion object.So the same goes for
var status = Boolean
– it’sBoolean
‘s companion object. And that’s why your function has to returnBoolean.Companion
, because it’s returningstatus
, and that’s whatstatus
is. It’s not a boolean value of true or false.There may be reasons why you’d want to do this – it’s very unlikely though, and the way you’re trying to use this (wanting a
Boolean
fromgetStatus
) suggests that this is a mistake, and you’re not familiar with the language. I’d really recommend running through the basic intro stuff to get a feel for how you define variables and their types, but this is probably what you wanted:I’ve added default values to each because you have to initialise them to something – if you have an
init
block where that happens, you can omit the values. If you’re assigning them right there, you can omit the types too (e.g.var id = -1
) unless you need to be more specific than the value is (e.g. if you want it to be a nullableInt?
type).You could also put these values in the constructor:
which would require the caller to provide some initial values. It depends what you want!
If you wanted, you could also replace the getter function by doing this:
which makes the
status
property public, but read-only from the outside.And just FYI, your
Model
class looks like your data for yourRecyclerView
‘s Adapter – it should not be aViewHolder
, those are completely separate things. Just use a basic class for yourModel
(or even better, adata class
) and use that in your data list.ViewHolder
s are special objects that get reused to display different data items – and by definition there’s usually more data items than there areViewHolder
s (that’s the whole point of aRecyclerView
– it recycles them). You shouldn’t be keeping them in a list, or storing individual items’ state in them.Keep your data items in a list, fetch the one at
position
inonBindViewHolder
, then display it in theViewHolder
you’re provided with. What you have right now isn’t going to work, so you’ll need to look at a tutorial for setting one up, e.g. this one from the docs. You need to store references to theView
s you’re using in the VH, like aTextView
and aCheckbox
, so you can do things likeholder.taskTextView.text = item.getTask()