https://kotlinlang.org logo
#compose
Title
# compose
a

A. Sachdeva

02/27/2021, 8:29 PM
Hi, I was trying to create notes app like screen and ran into some bugs and crashes, probably due to my implementation. below file contains the code. • My ui would  consists of a list of notes, each note consisting of text, a button to edit(that will just change notes text directly on click) , a button to delete  and an overall toast on item click. • so from what i know, for this  i would need an item ui composable, a list ui composable , a state controller composable(state hoisting i guess?) and finally setting all of this to setContentView, right? • so i did all this .the  list was showing correctly. I was also using
SnapshotStateList
directly for learning purposes instead of using
mutableStatelistof.
But: 1. the  on click log and delete action seems to work, but when done too quickly, started giving index out of bounds. is it because of my implementation? 2. my main goal was to show a toast on item click, but could not access 
LocalContext.current
 , so had to test with logs. how to get context? 3. onEdit was not working immediately, but would show effect after scrolling. if it was recycler view, i would say a classic problem of adapter not reflecting latest changes due to lack of notifydatasetchanged call. so again, what did i missed?
j

jaqxues

02/27/2021, 9:21 PM
The edit does too much.
list.remove(element)
should do val ctx = LocalContext.current val onClick = { ... Toast.makeText(ctx,...) } The edit modifies the element itself, which cannot be picked up by the statelist. Your objects have to be immutable, you have to delete add or replace entire elements in the list to work correctly This code in general is somehwat.... Questionable. You should really look at compose/kotlin samples to get a feeling of how this should work and be used
a

A. Sachdeva

02/27/2021, 9:39 PM
hey thanks again @jaqxues!, it worked. this seems to be such a beginner bug, i remember running into the exact same scenario when working with recycler view when i first started android dev, hehe. changed to remove and add new element and it worked(although is it possible to get the position through the child itself, and not have to iterate for each click, like recycler adapter gives?) and for the LocalContext.current, i am getting this weird error message
j

jaqxues

02/27/2021, 9:48 PM
As i said,store the context in a variable, then use that variable in the onclick
a

A. Sachdeva

02/28/2021, 6:16 AM
once again, it worked , thanks! any particular reason why context is not available inside the lambda?and would it work, if the children themselves pass the context , like the ui model to the onclick?
4 Views