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
2 changes: 2 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ dependencies {
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
val fragment_version = "1.8.9"
implementation("androidx.fragment:fragment-ktx:$fragment_version")

Choose a reason for hiding this comment

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

불필요한 라이브러리 같은데, 추가하신 이유가 있을까요?

Copy link
Author

Choose a reason for hiding this comment

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

처음에 fragment를 이용하여 풀어보려 했는데 결국엔 사용하지 않았습니다. 그 과정에서 미처 지우지 못했습니다

}

Choose a reason for hiding this comment

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

newline에 대해 알아보시면 좋을 것 같습니다

23 changes: 23 additions & 0 deletions app/src/main/java/com/example/android_25_2/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.example.android_25_2

import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import androidx.activity.enableEdgeToEdge
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

data class ListItem(val name: String)

Choose a reason for hiding this comment

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

별도의 class이니 별도의 파일로 분리하는게 좋아보이네요

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -16,5 +21,23 @@ class MainActivity : AppCompatActivity() {
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}

val editText: EditText = findViewById(R.id.edit_text)
val addButton : Button = findViewById(R.id.button_add)
val itemList = mutableListOf<ListItem>()
val recyclerView: RecyclerView = findViewById(R.id.recycler_view)
val adapter = NameAdapter(itemList)

recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = adapter

addButton.setOnClickListener {
val name = editText.text.toString()
if (name.isNotEmpty()){
itemList.add(ListItem(name))
adapter.notifyItemInserted(itemList.size -1)

Choose a reason for hiding this comment

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

itemList.sizeitemList.lastIndex는 무슨 차이일까요?

Copy link
Author

Choose a reason for hiding this comment

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

itemList.size는 리스트의 전체 요소 개수이며, itemList.lastIndex는 리스트의 마지막 요소의 인덱스입니다

editText.text.clear()
}
}
}
}
49 changes: 49 additions & 0 deletions app/src/main/java/com/example/android_25_2/NameAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.example.android_25_2

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView

class NameAdapter(val items: MutableList<ListItem>) : RecyclerView.Adapter<NameAdapter.ViewHolder>() {

inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val nameText: TextView = itemView.findViewById(R.id.text_name)
val rootView: ConstraintLayout = itemView.findViewById(R.id.root_view)

fun addText(item: ListItem) {
nameText.text = item.name


}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item, parent, false)
return ViewHolder(view)
}

override fun onBindViewHolder(viewHolder: ViewHolder, position: Int){
viewHolder.nameText.text = items[position].name
viewHolder.rootView.setOnClickListener {
AlertDialog.Builder(viewHolder.rootView.context)

Choose a reason for hiding this comment

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

몇가지 질문이 있습니다

  1. context란 무엇인가요
  2. viewHolder.rootView.context는 어떤 context일까요
  3. 다른 방법으로 context를 받을 수는 없을까요

Copy link
Author

Choose a reason for hiding this comment

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

그 작업을 어느 activity에서 할건지 알려주는 것이라고 생각합니다
viewHolder.rootView.context는 RecyclerView가 속한 Activity의 Context입니다
현재는 view에서 가져오고 있지만 생성자로 받을 수 있으며 this로 지명할 수 있습니다

.setTitle(R.string.dialog_title)
.setMessage(R.string.dialog_message)
.setPositiveButton(R.string.dialog_positive_message) { dialog, _ ->
items.removeAt(position)
notifyItemRemoved(position)
Copy link
Collaborator

Choose a reason for hiding this comment

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

질문이 많으니 저도 질문
notifyItemRemoved(position) 을 호출하지 않으면 어떻게 될까요?

Copy link
Author

Choose a reason for hiding this comment

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

item,removeAt(position)으로 리스트에서 제거는 되지만 내용이 갱신되지 않아 오류가 발생합니다

dialog.dismiss()
}
.setNegativeButton(R.string.dialog_negative_message, null)
.show()
}
}

override fun getItemCount() = items.size
}

38 changes: 38 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,42 @@
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/edit_text"
android:layout_width="330dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:hint="@string/editText_hint"
android:inputType="text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button_add"

Choose a reason for hiding this comment

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

naming convention 잘 지키셨네요!
다만 조금 더 명확하게 이름을 짓는다면, button_add_item은 어떨까 싶네요

android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:backgroundTint="@color/yellow"
android:text="+"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintTop_toBottomOf="@+id/edit_text"
app:layout_constraintBottom_toTopOf="@+id/button_add"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp" />



</androidx.constraintlayout.widget.ConstraintLayout>
21 changes: 21 additions & 0 deletions app/src/main/res/layout/item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFA500"
android:id="@+id/root_view"
android:padding="16dp">

<TextView
android:id="@+id/text_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="@color/white"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="yellow">#FACC00</color>
<color name="Orange">#F99E14</color>
</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<resources>
<string name="app_name">Android_25-2</string>
<string name="editText_hint">add</string>
<string name="dialog_title">Delet</string>
<string name="dialog_message">Sure?</string>
<string name="dialog_positive_message">Yes</string>
<string name="dialog_negative_message">No</string>
</resources>
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ appcompat = "1.7.1"
material = "1.12.0"
activity = "1.11.0"
constraintlayout = "2.2.1"
fragment_version = "1.8.9"

Choose a reason for hiding this comment

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

여기도 불필요해보입니다


[libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
Expand Down