Untitled
unknown
kotlin
5 months ago
2.4 kB
3
Indexable
package com.example.todolistapplicationtwo class MainActivity : AppCompatActivity() { private lateinit var todoAppRepo: TodoApplicationRepository private val todoList = mutableListOf<TodoItem>() private lateinit var adapter: TodoAdapter override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) todoAppRepo = TodoApplicationRepository(this) val todoTitle = findViewById<EditText>(R.id.todoTitle) val todoDescription = findViewById<EditText>(R.id.todoDescription) val todoPriority = findViewById<Spinner>(R.id.todoPriority) val addTodoButton = findViewById<Button>(R.id.addTodoButton) val todoRecyclerView = findViewById<RecyclerView>(R.id.todoRecyclerView) // Priority Levels val priorities = listOf("Low", "Medium", "High") val priorityAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, priorities) todoPriority.adapter = priorityAdapter // Load data from the database todoList.clear() // Clear the existing list before adding new data todoList.addAll(todoAppRepo.getAllTodos()) // Get all todos from the database // RecyclerView setup adapter = TodoAdapter(todoList, todoAppRepo) todoRecyclerView.layoutManager = LinearLayoutManager(this) todoRecyclerView.adapter = adapter // Add To-Do button click addTodoButton.setOnClickListener { val title = todoTitle.text.toString() val description = todoDescription.text.toString() val priority = todoPriority.selectedItem.toString() if (title.isBlank() || description.isBlank()) { Toast.makeText(this, "Please fill in all fields", Toast.LENGTH_SHORT).show() return@setOnClickListener } // Create a new TodoItem val newTodo = TodoItem(0, title, description, priority, false) val id = todoAppRepo.addTodo(newTodo) if (id > 0) { val savedTodo = newTodo.copy(id = id.toInt()) todoList.add(savedTodo) adapter.notifyItemInserted(todoList.size - 1) } todoTitle.text.clear() todoDescription.text.clear() todoPriority.setSelection(0) } } }
Editor is loading...
Leave a Comment