I'm trying to create a to-do list. first click menu item (add) and alertdialog write edittext and save but I'm trying to get editable text with alarm box but i get always same number.
i write bread but same number generate photo
here is a menu code
R.id.add -> {
val mDialogView = LayoutInflater.from(this).inflate(R.layout.dialog_add_todo, null)
AlertDialog.Builder(this).setView(mDialogView).setTitle("ADD TODO").setPositiveButton("Save"){
dialogInterface, i ->
val todoTitle = R.id.et_dialog_add.toString()
if(todoTitle.isNotEmpty()) {
val todo = Todo(todoTitle)
todoAdapter.addTodo(todo)
alert dialog:
<EditText
android:id="@ id/et_dialog_add"
android:hint="Buy a Bread"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</EditText>
here is adapters:
fun addTodo(todo: Todo) {
todos.add(todo)
notifyItemInserted(todos.size -1)
}
CodePudding user response:
This is your problem:
val todoTitle = R.id.et_dialog_add.toString()
This is not how you get the text value from an EditText
field. You need to first use findViewById()
to get a reference to the EditText
, and then you can use its text
property to get what the user entered:
val todoTitleView = mDialogView.findViewById<EditText>(R.id.et_dialog_add)
val todoTitle = todoTitleView.text