이번 글에서는 작성한 할 일의 상세 보기를 만들어봄.
여기서는 다이얼로그를 사용해서 간단하게 보여주기만 할 거임.
MainAdapter에서 MainActivity의 메서드를 사용하기 위해 인자로 this(MainActivity)를 넘겨줌.
그리고 아래에 todoDialog를 작성해 줌.
todoDialog에서의 매개 변수에는 클릭한 항목의 할 일 Text를 넘겨 받을 것임.
class MainActivity : AppCompatActivity(), View.OnClickListener {
// adapter 선언
val mainAdapter = MainAdapter(this, todoList)
override fun onCreate(savedInstanceState: Bundle?) {
...
}
override fun onClick(view : View?) {
...
}
// 키보드 내리기
fun hideKeyboard(act: Activity){
...
}
// 다이얼로그
fun todoDialog(todo : String) {
val builder = AlertDialog.Builder(this)
builder.setTitle("할 일")
builder.setMessage("$todo")
builder.show()
}
}
MainActivity.kt
MainAdapter에서는 매개 변수로 MainActivity를 받음.
그리고 할 일 항목을 클릭했을 경우 다이얼로그를 띄우도록 만들어줌.
todoDialog의 인자로는 클릭한 항목의 할 일 Text를 보내줌.
class MainAdapter(val mainActivity : MainActivity, val todoList : MutableList<TodoEntity>) : RecyclerView.Adapter<MainAdapter.ViewHolderClass>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainAdapter.ViewHolderClass {
...
}
override fun onBindViewHolder(holder: MainAdapter.ViewHolderClass, position: Int) {
...
// 할 일 항목을 클릭했을 경우
holder.todo.setOnClickListener {
mainActivity.todoDialog(holder.todo.text.toString())
}
}
override fun getItemCount(): Int {
...
}
inner class ViewHolderClass(view : View) : RecyclerView.ViewHolder(view) {
...
}
}
MainAdapter.kt
이제 실행 후 항목을 클릭해보면 해당 할 일의 내용이 나오고 다른 곳을 클릭(터치)하면 다이얼로그가 사라짐.
지금까지 매우 간단한 TodoList 앱을 만들어봄.
The End.
'📱 > 📘Project' 카테고리의 다른 글
[Kotlin] Note 앱 만들기3 (1) | 2023.01.11 |
---|---|
[Kotlin] Note 앱 만들기2 (0) | 2023.01.10 |
[Kotlin] Note 앱 만들기1 (0) | 2023.01.09 |
[Kotlin] TodoList 앱 만들기2 (2) | 2022.12.29 |
[Kotlin] TodoList 앱 만들기1 (0) | 2022.12.29 |