Office Hours — Today, June 13

Thursday, June 11

Jun 13
3:55 PM
Mark M.
has entered the room
Mark M.
turned on guest access
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
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
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
and have the callback call notifyDataSetChanged()
alfonso
i dont understand
Mark M.
here, I have a viewholder that needs to respond to click events
I pass a function type that serves as a callback, and the viewholder calls that when the row is clicked
the viewholder does not know or care what the end impact of the click is -- it just calls the function type
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()?
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
that sounds like a problem with your adapter and viewholder setup
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
"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
personally, I try to keep this sort of interactive widget out of recycled rows, because it gets confusing to keep track of state
4:20 PM
alfonso
View paste
is there a shorter way of writing this:
val isChecked = myObj?.isChecked
if (isChecked != null) { checkbox.isChecked = isChecked } else { Log.e("error", "isChecked is null" }
Mark M.
View paste
val isChecked = myObj?.isChecked

isChecked?.let { checkbox.isChecked = it } ?: { Log.e("error", "isChecked is null" }
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
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
checkbox.isChecked = myObj?.isChecked ?: false
that would update the checkbox, handling the null myObj by using false as a default value
4:30 PM
alfonso
thanks
alfonso
has left the room
4:55 PM
Mark M.
turned off guest access

Thursday, June 11

 

Office Hours

People in this transcript

  • alfonso
  • Mark Murphy