Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.
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 {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EditText는 변수로 따로 선언해주시고 Button은 따로 선언 안해주셨네용
기능적으로 문제 있는 거는 아니지만 좀 더 가독성 있는 코드를 작성하기 위해..

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)

Choose a reason for hiding this comment

The 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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {

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
Expand Up @@ -5,15 +5,32 @@
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">

<TextView
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="내용을 입력하세요"
app:layout_constraintEnd_toStartOf="@+id/addButton"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
android:text="+"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />

</androidx.constraintlayout.widget.ConstraintLayout>
8 changes: 8 additions & 0 deletions assignment_week_7/app/src/main/res/layout/item_name.xml
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" />
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Tue Dec 03 14:18:12 KST 2024
#Tue Dec 03 15:58:39 KST 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 0 additions & 3 deletions newProject/.idea/.gitignore

This file was deleted.

1 change: 0 additions & 1 deletion newProject/.idea/.name

This file was deleted.

This file was deleted.