-
Notifications
You must be signed in to change notification settings - Fork 0
7주차 과제 PR입니다. #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| package com.example.assignment_week_7 | ||
|
|
||
| import android.os.Bundle | ||
| import android.widget.EditText | ||
| import android.widget.Button | ||
| import androidx.activity.enableEdgeToEdge | ||
| import androidx.appcompat.app.AlertDialog | ||
| import androidx.appcompat.app.AppCompatActivity | ||
| import androidx.core.view.ViewCompat | ||
| import androidx.core.view.WindowInsetsCompat | ||
| import androidx.recyclerview.widget.LinearLayoutManager | ||
| import androidx.recyclerview.widget.RecyclerView | ||
|
|
||
| class MainActivity : AppCompatActivity() { | ||
| private lateinit var adapter: NameAdapter | ||
| private val names = mutableListOf<String>() | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| enableEdgeToEdge() | ||
| setContentView(R.layout.activity_main) | ||
| ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets -> | ||
| val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()) | ||
| v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom) | ||
| insets | ||
| } | ||
|
|
||
| setupRecyclerView() | ||
| setupAddButton() | ||
| } | ||
|
|
||
| private fun setupRecyclerView() { | ||
| adapter = NameAdapter( | ||
| names, | ||
| onItemClick = { position -> showDeleteDialog(position) }, | ||
| onItemLongClick = { position -> showEditDialog(position) } | ||
| ) | ||
| findViewById<RecyclerView>(R.id.recyclerView).apply { | ||
| layoutManager = LinearLayoutManager(this@MainActivity) | ||
| adapter = this@MainActivity.adapter | ||
| } | ||
| } | ||
|
|
||
| private fun setupAddButton() { | ||
| val nameEditText = findViewById<EditText>(R.id.editText) | ||
| findViewById<Button>(R.id.addButton).setOnClickListener { | ||
| val name = nameEditText.text.toString() | ||
| if (name.isNotEmpty()) { | ||
| names.add(name) | ||
| adapter.notifyItemInserted(names.size - 1) | ||
| nameEditText.text.clear() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun showDeleteDialog(position: Int) { | ||
| AlertDialog.Builder(this) | ||
| .setTitle("삭제") | ||
| .setMessage("${names[position]}을(를) 삭제하시겠습니까?") | ||
| .setPositiveButton("삭제") { _, _ -> | ||
| names.removeAt(position) | ||
| adapter.notifyItemRemoved(position) | ||
| } | ||
| .setNegativeButton("취소", null) | ||
| .show() | ||
| } | ||
|
|
||
| private fun showEditDialog(position: Int) { | ||
| val editText = EditText(this) | ||
| editText.setText(names[position]) | ||
|
|
||
| AlertDialog.Builder(this) | ||
| .setTitle("수정") | ||
| .setView(editText) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. edittext가 들어간 커스텀 다이얼로그를 만들어보는 거는 어떨까용?? |
||
| .setPositiveButton("확인") { _, _ -> | ||
| val newName = editText.text.toString() | ||
| if (newName.isNotEmpty()) { | ||
| names[position] = newName | ||
| adapter.notifyItemChanged(position) | ||
| } | ||
| } | ||
| .setNegativeButton("취소", null) | ||
| .show() | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,35 @@ | ||||||
| package com.example.assignment_week_7 | ||||||
|
|
||||||
| import android.view.LayoutInflater | ||||||
| import android.view.View | ||||||
| import android.view.ViewGroup | ||||||
| import android.widget.TextView | ||||||
| import androidx.recyclerview.widget.RecyclerView | ||||||
|
|
||||||
| class NameAdapter( | ||||||
| private val names: MutableList<String>, | ||||||
| private val onItemClick: (Int) -> Unit, | ||||||
| private val onItemLongClick: (Int) -> Unit | ||||||
| ) : RecyclerView.Adapter<NameAdapter.ViewHolder>() { | ||||||
|
|
||||||
| class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| val textView: TextView = view.findViewById(R.id.nameTextView) | ||||||
| } | ||||||
|
|
||||||
| override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | ||||||
| val view = LayoutInflater.from(parent.context) | ||||||
| .inflate(R.layout.item_name, parent, false) | ||||||
| return ViewHolder(view) | ||||||
| } | ||||||
|
|
||||||
| override fun onBindViewHolder(holder: ViewHolder, position: Int) { | ||||||
| holder.textView.text = names[position] | ||||||
| holder.itemView.setOnClickListener { onItemClick(position) } | ||||||
| holder.itemView.setOnLongClickListener { | ||||||
| onItemLongClick(position) | ||||||
| true | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| override fun getItemCount() = names.size | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <TextView xmlns:android="http://schemas.android.com/apk/res/android" | ||
| android:id="@+id/nameTextView" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="wrap_content" | ||
| android:padding="16dp" | ||
| android:textSize="16sp" | ||
| android:background="#FFA726" /> |
This file was deleted.
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EditText는 변수로 따로 선언해주시고 Button은 따로 선언 안해주셨네용
기능적으로 문제 있는 거는 아니지만 좀 더 가독성 있는 코드를 작성하기 위해..