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)
}
}
}