You are currently viewing list doesn’t update when deleting item

list doesn’t update when deleting item

I’ve got a ViewModel where I put all the logic code The problem is when I click on the delete button then the list doesn’t update. When I manually refresh then I can see that de item is deleted.

Viewmodel

@HiltViewModel class CommentViewModel u/Inject constructor(private val _repo : Repository): ViewModel() { var isLoading = mutableStateOf(false) //private var _getComments = MutableLiveData<List<Comment>>() //var getComments: LiveData<List<Comment>> = _getComments private var _getComments = MutableStateFlow(listOf<Comment>()) val getComments: StateFlow<List<Comment>> get() = _getComments ////delete //private var _deleteComment: MutableLiveData<Comment> = MutableLiveData<Comment>() //var deleteComment: LiveData<Comment> = _deleteComment ////add //private var _addComment: MutableLiveData<CreateCommentViewModel> = MutableLiveData<CreateCommentViewModel>() //var addComment: LiveData<CreateCommentViewModel> = _addComment init { getComments } suspend fun getComments(id: Int): Resource<List<Comment>> { val result = _repo.getCommentsByDocreviewId(id) viewModelScope.launch(Dispatchers.Default) { if (result is Resource.Success) { isLoading.value = true _getComments.emit(result.data!!) } } return result } suspend fun deleteComment(id: Int) { val result = _repo.deleteComment(id) viewModelScope.launch(Dispatchers.Default) { if (result is Resource.Success) { isLoading.value = true _getComments.emit(listOf(result.data!!)) } } } 

I tried playing with mutablelivedata, mutablestate and mutablestateflow without any success. Am I doing something wrong here.

messagecard composable where I Delete the item

fun MessageCard( comment: Comment, viewModel: CommentViewModel = hiltViewModel() ) { val scope = rememberCoroutineScope() val context = LocalContext.current Row(modifier = Modifier.padding(all = 8.dp)) { Image( painter = painterResource(R.drawable.ic_avatar), contentDescription = null, modifier = Modifier .size(40.dp) .clip(CircleShape) .border(1.5.dp, MaterialTheme.colors.secondaryVariant, CircleShape) ) Spacer(modifier = Modifier.width(8.dp)) Card(modifier = Modifier .fillMaxWidth() .wrapContentHeight()) { var isExpanded by remember { mutableStateOf(false) } var showIconBtns by remember { mutableStateOf(false) } val surfaceColor by animateColorAsState( if (isExpanded) MaterialTheme.colors.primary else MaterialTheme.colors.surface, ) // We toggle the isExpanded variable when we click on this Column Column(modifier = Modifier.clickable () { showIconBtns = !showIconBtns isExpanded = !isExpanded }) { Text( text = "test", color = MaterialTheme.colors.secondaryVariant, style = MaterialTheme.typography.subtitle2 ) Spacer(modifier = Modifier.height(4.dp)) Text( text = comment.content, modifier = Modifier.padding(all = 4.dp), maxLines = if (isExpanded) Int.MAX_VALUE else 1, style = MaterialTheme.typography.body2 ) if (showIconBtns){ Row() { IconButton(onClick = { /*TODO*/}) { Icon( Icons.Default.Edit, contentDescription = "edit" ) } IconButton(onClick = { /*TODO*/ scope.launch { viewModel.deleteComment(comment.id) // if (result is Resource.Success) { // Toast.makeText(context, "delete comments success!", Toast.LENGTH_SHORT).show() // // } else if (result is Resource.Error) { // Toast.makeText(context, "Error: ${result.message}", Toast.LENGTH_SHORT) // .show() // } } }) { Icon( Icons.Default.Delete, contentDescription = "delete" ) } } } } } } } 

And this is how i show a list of comments

 LazyColumn { items(getAllComments.value.size) { index -> MessageCard(getAllComments.value[index]) } } 

Thanks for giving feedback and helping me out. 🙂

submitted by /u/akhandafm17
[link] [comments]