Skip to content
Share
Explore

5️⃣ Create EditAnimalActivity.kt

This screen:
receives the position of the clicked animal
loads the name from AnimalRepository.animals[position]
lets the user change it
writes it back and closes
package com.example.adapter1

import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity

class EditAnimalActivity : AppCompatActivity() {

private val TAG = "EDIT_ANIMAL"

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_edit_animal)

val position = intent.getIntExtra("position", -1)
Log.d(TAG, "onCreate: editing position = $position")

val editName = findViewById<EditText>(R.id.editAnimalName)
val btnSave = findViewById<Button>(R.id.btnSaveAnimal)

if (position != -1) {
val currentName = AnimalRepository.animals[position]
editName.setText(currentName)
}

btnSave.setOnClickListener {
if (position != -1) {
val newName = editName.text.toString()
AnimalRepository.animals[position] = newName
Log.d(TAG, "onClick: saved new name = $newName at $position")
}
finish() // go back to MainActivity
}
}
}
Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.