How to pass a MutableList from a ViewModel to the MainActivity

Sorry, if I’m not using the correct terms. I’m really a newbie (or oldie, depending how you would like to describe it).

Anyway, I have a quite simple app for creating, let’s say, incidents. User can add, edit and delete them. They are stored in Back4App backend. I used some example found in the Internet, edited it to meet my needs and it was quite ok. Everything was working. However, the list was implemented as a dictionary, which I later changed to MutableList. After that the mainactivity won’t show the incident list any more. Adding a new incident is still possible and it is stored correctly, but nothing is shown on the list. I’ve debugged and the data fetch is working, but passing the list to the mainactivity seems not to work.

Im out of ideas and competence (if there were any in the first place). I guess the problem is somewhere in the MainActivity.kt or/and AppViewModel.kt files, I’ll post both here. I don’t know the essential parts, so whole files here.

class AppViewModel : ViewModel() {
    val CallForHelps: MutableList<CallForHelp> = mutableListOf()

    init {
        val query = com.parse.ParseQuery.getQuery<com.parse.ParseObject>("CallForHelp")
        query.orderByDescending("createdAt")
        query.findInBackground { cfhs, e ->
            if (e == null) {
                for (parseCFH in cfhs) {
                    val cfh = CallForHelp(
                        objectId = parseCFH.objectId,
                        title = parseCFH.getString("Title")!!,
                        description = parseCFH.getString("Description")!!,
                        status = parseCFH.getString("Status")!!,
                        createdAt = parseCFH.createdAt,
                        loca = null,
                    )
                    this.CallForHelps.plus(cfh)
                }
            } else {
                println("Error: ${e.message}")
            }
        }
    }

    companion object {
        @Volatile
        private var instance: AppViewModel? = null

        fun getInstance(): AppViewModel {
            return instance ?: synchronized(this) {
                instance ?: AppViewModel().also { instance = it }
            }
        }
    }
}

And

class MainActivity : ComponentActivity() {
    private val viewM = AppViewModel.getInstance()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val context = this as Context

        setContent {
            MainActivityContent(
                viewModel = viewM,
                onCfhListItemClick = {
                    val intent = Intent(context, FormActivity::class.java)
                    intent.putExtra("index", viewM.CallForHelps.indexOf(it))
                    context.startActivity(intent)
                },
                onAddClick = {
                    val intent = Intent(context, FormActivity::class.java)
                    intent.putExtra("index", 0)
                    context.startActivity(intent)
                },
            )
        }
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MainActivityContent(
    viewModel: AppViewModel,
    onCfhListItemClick: (cfh: CallForHelp) -> Unit,
    onAddClick: () -> Unit,
) {
    val cfhs = viewModel.CallForHelps
    MaterialTheme {
        Scaffold(
            topBar = { TopAppBar(title = { Text("Help requests") }) },
            floatingActionButton = {
                ExtendedFloatingActionButton(
                    onClick = { onAddClick() },
                    icon = { Icon(Icons.Filled.Add, contentDescription = "Add") },
                    text = { Text("Add") },
                )
            },
        ) { contentPadding ->
            Box(modifier = Modifier.padding(contentPadding)) {
                NoteList(cfhs, onCfhListItemClick = { onCfhListItemClick(it) })
            }
        }
    }
}

@Composable
fun CfhListItem(cfh: CallForHelp, onCfhListItemClick: (cfh: CallForHelp) -> Unit) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .clickable(onClick = { onCfhListItemClick(cfh) })
            .padding(16.dp),
        verticalAlignment = Alignment.CenterVertically,
    ) {
        val imageModifier = Modifier
            .size(50.dp)

        when (cfh.status) {
            "Open" ->
                Image(
                    painterResource(R.drawable.open),
                    contentDescription = stringResource(id = R.string.open_description),
                    modifier = imageModifier,
                )

            "Ongoing" ->
                Image(
                    painter = painterResource(R.drawable.ongoing),
                    contentDescription = stringResource(id = R.string.ongoing_description),
                    modifier = imageModifier,
                )

            "Closed" ->
                Image(
                    painter = painterResource(R.drawable.closed),
                    contentDescription = stringResource(id = R.string.closed_description),
                    modifier = imageModifier,
                )
        }

        Spacer(modifier = Modifier.width(8.dp))
        Column {
            Text(text = cfh.title, fontSize = 18.sp)
            Spacer(modifier = Modifier.height(4.dp))
            Text(
                text = SimpleDateFormat("dd.MM.YYYY HH:mm").format(cfh.createdAt),
                fontSize = 14.sp,
                maxLines = 1,
                overflow = TextOverflow.Ellipsis,
            )
            Spacer(modifier = Modifier.height(4.dp))
            Text(
                text = cfh.description,
                fontSize = 14.sp,
                maxLines = 1,
                overflow = TextOverflow.Ellipsis,
            )
            Spacer(modifier = Modifier.height(4.dp))
            Text(
                text = "Link placeholder",
                fontSize = 14.sp,
                maxLines = 1,
                overflow = TextOverflow.Ellipsis,
            )
        }
    }
}

@Composable
fun NoteList(cfhs: MutableList<CallForHelp>, onCfhListItemClick: (cfh: CallForHelp) -> Unit) {
    LazyColumn {
        items(cfhs) { cfh ->
            CfhListItem(cfh, onCfhListItemClick = onCfhListItemClick)
        }
    }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật