Jun 13 | 3:55 PM |
Mark M. | has entered the room |
Mark M. | turned on guest access |
Jun 13 | 4:05 PM |
alfonso | has entered the room |
alfonso |
hey marjk
|
Mark M. |
hello, alfonso!
|
alfonso |
hey mark
|
Mark M. |
how can I help you today?
|
alfonso |
I have an adapter in kotlin. I have a bind method in my inner viewholder class. How do I call adapter.notifyDataSetChanged() from my bind methood?
|
Mark M. |
off the cuff, that does not sound good -- it feels like you would get into an infinite loop
|
Jun 13 | 4:10 PM |
Mark M. |
your adapter binds your viewholder, which tells the adapter that the data change, which binds your viewholder, which tells the adapter that the data change, which binds your viewholder, ...
|
alfonso |
i need to display a correct state for my checkboxes
|
Mark M. |
technically, there is nothing stopping you from passing your RecyclerView.Adapter to your bind() method, or to the RecyclerView.ViewHolder's constructor
|
Mark M. |
but displaying the correct state for the checkboxes hopefully does not involve notifyDataSetChanged() just because you bound data to a viewholder
|
alfonso |
inside bind I set an onclicklistener. when it is clicked I set the model isChecked=true and call notifyDataSetChanged
|
Mark M. |
I would pass a callback into the viewholder that you call when the checkbox is checked
|
Mark M. |
and have the callback call notifyDataSetChanged()
|
alfonso |
i dont understand
|
Mark M. | |
Mark M. |
here, I have a viewholder that needs to respond to click events
|
Mark M. |
I pass a function type that serves as a callback, and the viewholder calls that when the row is clicked
|
Mark M. |
the viewholder does not know or care what the end impact of the click is -- it just calls the function type
|
Jun 13 | 4:15 PM |
alfonso |
why not set the click listener on the checkbox inside of bind
|
Mark M. |
I do have the listener in the viewholder... but the listener just calls the function type
|
alfonso |
is there a way to do it without calling notifyDataSetChanged()?
|
Mark M. |
I am not certain what "it" is, exactly -- why do you feel that you need to call notifyDataSetChanged()?
|
Mark M. |
you would only do that if you were replacing the entire contents of the RecyclerView
|
alfonso |
selecting a checkbox will select another checkbox because the view is recycled
|
Mark M. |
or otherwise making *major* changes to its contents
|
Mark M. |
that sounds like a problem with your adapter and viewholder setup
|
Mark M. |
while rows do get recycled, one checkbox should not be affecting another checkbox in another row
|
alfonso |
it will because the checkbox is part of the row. the row gets recycled, so the checkbox (and its state) is used again.
|
Mark M. |
correct, but that is still just one checkbox
|
Mark M. |
"selecting a checkbox will select another checkbox" implies two checkboxes
|
alfonso |
yes, i select one and another is selected too because of the recycling
|
Mark M. |
then that sounds like a problem with your adapter and viewholder setup
|
Mark M. |
personally, I try to keep this sort of interactive widget out of recycled rows, because it gets confusing to keep track of state
|
Jun 13 | 4:20 PM |
alfonso |
View paste
|
Mark M. |
View paste
|
Mark M. |
though if it is an error for myObj to be null (based on your Log.e()), then perhaps you should be taking steps to prevent myObj from being null
|
alfonso |
myObj comes from the server and I cant predict if its going to be null or not. if shouldnt be but the api can make mistakes
|
Mark M. |
but, shouldn't you be filtering out bad values before they get to the UI?
|
alfonso |
thats what im doing with my if statement
|
Jun 13 | 4:25 PM |
Mark M. |
my assumption is that this code snippet is in a viewholder -- if so, then my point is that whatever has myObj should not be in the data that is in the RecyclerView
|
alfonso |
that means at some point I would have to make an if check and prevent nulls from entering the list
|
Mark M. |
that would be the responsibility of a repository or viewmodel -- clean up broken data so it does not harm the user experience
|
alfonso |
yes but somewhere you will need to check if an object is null or not. my question was just if kotlin had a shorter way of doing that. It looks like the let isnt much shorter
|
Mark M. |
the problem really is with your log statement, and wanting to leave checkbox alone if myObj is null, rather than use a default value
|
Mark M. |
checkbox.isChecked = myObj?.isChecked ?: false
|
Mark M. |
that would update the checkbox, handling the null myObj by using false as a default value
|
Jun 13 | 4:30 PM |
alfonso |
thanks
|
alfonso | has left the room |
Jun 13 | 4:55 PM |
Mark M. | turned off guest access |