Hi I am making a notes app and reading the database works but when I try to update the database and so edit the note it doesn’t work and I don’t know why so can you help? I am sharing snippets.
fun updateData(index:Int?,newNoteTitle:String?,newNoteContent: String?){
val db=this.writableDatabase
val values=ContentValues()
values.put(NOTE_TITLE_COL,newNoteTitle)
values.put(NOTE_CONTENT_COL,newNoteContent)
db.update(TABLE_NAME,values,"$ID_COL=?", arrayOf(index.toString()))
db.close()
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ShowDetails(context: Context) {
//lateinit var NoteList: List<Store>
val NoteList=remember{ mutableStateListOf<Store>() }
//NoteList = ArrayList<Store>()
val dbHandler: `DB Handler` = `DB Handler`(context)
NoteList.addAll(dbHandler.readData() ?: emptyList())
//NoteList = dbHandler.readData()!!
//val main= MainActivity()
LazyColumn(modifier= Modifier
.absoluteOffset(0.dp, 200.dp)
.fillMaxHeight()){
itemsIndexed(NoteList) { index, item ->
val currentTitle= remember { mutableStateOf(NoteList[index].NoteTitle) }
val currentContent= remember { mutableStateOf(NoteList[index].NoteContent) }
TextField(value = currentTitle.value, onValueChange = {newText -> currentTitle.value=newText},modifier = Modifier.offset(55.dp))
TextField(value = currentContent.value, onValueChange = {newText -> currentContent.value=newText}, modifier = Modifier.offset(55.dp))
val navigate= Intent(context, NoteDetails::class.java)
navigate.putExtra("key2", index)
Button(
onClick = {
val updatedTitle=currentTitle.value
val updatedContent=currentContent.value
//dbHandler.updateData(index,updatedTitle,updatedContent)
dbHandler.updateData(index, updatedTitle, updatedContent)
}, modifier = Modifier,
shape = RoundedCornerShape(15.dp)
)
{
Text(
text = "Save Changes",
fontSize = 30.sp
)
}
Button(
onClick = {
}, modifier = Modifier
.absoluteOffset(y=100.dp),
shape = RoundedCornerShape(15.dp)
)
{
Text(
text = "Delete",
fontSize = 30.sp
)
}
}
}
}
The problem is when I click the button by the way every value is correct I have logged them and debugged them.
When I click Save Changes nothing happens.
fun updateData(index:Int?,newNoteTitle:String?,newNoteContent: String?){
val db=this.writableDatabase
val values=ContentValues()
values.put(NOTE_TITLE_COL,newNoteTitle)
values.put(NOTE_CONTENT_COL,newNoteContent)
db.update(TABLE_NAME,values,"$ID_COL=?", arrayOf(index.toString()))
db.close()
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ShowDetails(context: Context) {
//lateinit var NoteList: List<Store>
val NoteList=remember{ mutableStateListOf<Store>() }
//NoteList = ArrayList<Store>()
val dbHandler: `DB Handler` = `DB Handler`(context)
NoteList.addAll(dbHandler.readData() ?: emptyList())
//NoteList = dbHandler.readData()!!
//val main= MainActivity()
LazyColumn(modifier= Modifier
.absoluteOffset(0.dp, 200.dp)
.fillMaxHeight()){
itemsIndexed(NoteList) { index, item ->
val currentTitle= remember { mutableStateOf(NoteList[index].NoteTitle) }
val currentContent= remember { mutableStateOf(NoteList[index].NoteContent) }
TextField(value = currentTitle.value, onValueChange = {newText -> currentTitle.value=newText},modifier = Modifier.offset(55.dp))
TextField(value = currentContent.value, onValueChange = {newText -> currentContent.value=newText}, modifier = Modifier.offset(55.dp))
val navigate= Intent(context, NoteDetails::class.java)
navigate.putExtra("key2", index)
Button(
onClick = {
val updatedTitle=currentTitle.value
val updatedContent=currentContent.value
//dbHandler.updateData(index,updatedTitle,updatedContent)
dbHandler.updateData(index, updatedTitle, updatedContent)
}, modifier = Modifier,
shape = RoundedCornerShape(15.dp)
)
{
Text(
text = "Save Changes",
fontSize = 30.sp
)
}
Button(
onClick = {
}, modifier = Modifier
.absoluteOffset(y=100.dp),
shape = RoundedCornerShape(15.dp)
)
{
Text(
text = "Delete",
fontSize = 30.sp
)
}
}
}
}
The problem is when I click the button by the way every value is correct I have logged them and debugged them.
When I click Save Changes nothing happens.