I'm a 16-year-old trying to build a productivity app. This is my first time every coding in Kotlin, using Android Studio, and developing an app, so please don't be too harsh. :)
What I Want
My app is a calendar app in which opening the app presents a calendarView that allows you to pick a date. Choosing the date opens a note template view containing a text field that the user can write their notes in. After writing their notes, the user can press the 'Back' button to return to the date choosing screen and begin the process of taking notes once again in another date. If a note for a date is already created, the note and it's contents are accessed and spit onto the note template.
Issue
Once I create my first note upon opening the app, it works fine and I can exit to the date screen with no problem. However, when I try to click another date to create a new Note, there is no response.
My Attempt To Fix The Issue
I tried troubleshooting to see what the issue was by creating a Toast popup everytime the DateChange listener detected a date was chosen. I found that my listener was not functioning at all after coming back to the date choosing screen after clicking the back button. I have even tried asking Chat GPT, but to no avail. I'm trying my hardest to find the issue but I'm losing hope fast. I will leave my code down below, please comment and let me know of any solutions or mistakes I have made. Thanks for your time!
Code begins below -
package com.effici.application
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.widget.Button
import android.widget.CalendarView
import android.widget.TextView
import android.widget.Toast
import androidx.constraintlayout.widget.ConstraintLayout
import com.google.android.material.textfield.TextInputEditText
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.date_screen)
val dateChoosingCalendar = findViewById<CalendarView>(R.id.dateChoosingCalendar)
lateinit var noteInputField: TextInputEditText
var notesList = arrayListOf<Note>()
lateinit var backButton: Button
dateChoosingCalendar.setOnDateChangeListener { _, year, month, dayOfMonth ->
val chosenDate = "$dayOfMonth-${month + 1}-$year"
var noteAlreadyCreated = false
var noteIndex = 0
for (i in 0 until notesList.size) {
if (chosenDate == notesList[i].date) {
noteAlreadyCreated = true
noteIndex = i
break
}
}
if (noteAlreadyCreated) {
println("Note already created.")
setContentView(R.layout.note_template)
noteInputField = findViewById<TextInputEditText>(R.id.noteTakingField)
backButton = findViewById<Button>(R.id.changeButton)
backButton.setOnClickListener {
notesList[noteIndex].text = noteInputField.text
setContentView(R.layout.date_screen)
noteInputField.setText(notesList[noteIndex].text)
}
} else {
notesList.add(Note(userinp = null, dateParam = chosenDate))
noteIndex = notesList.size - 1
setContentView(R.layout.note_template)
noteInputField = findViewById<TextInputEditText>(R.id.noteTakingField)
backButton = findViewById<Button>(R.id.changeButton)
println("A note for $chosenDate is added.")
backButton.setOnClickListener {
notesList[noteIndex].text = noteInputField.text
setContentView(R.layout.date_screen)
}
}
}
}
}
class Note(val userinp: Editable?, val dateParam: String)
{
var text: Editable? = userinp
var date = dateParam
}