-
Notifications
You must be signed in to change notification settings - Fork 1
[김예란_Android] 2주차 과제 제출 #2
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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") | ||
| } | ||
|
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. newline에 대해 알아보시면 좋을 것 같습니다 |
||
| 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) | ||
|
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. 별도의 class이니 별도의 파일로 분리하는게 좋아보이네요 |
||
| class MainActivity : AppCompatActivity() { | ||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
|
|
@@ -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) | ||
|
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.
Author
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. itemList.size는 리스트의 전체 요소 개수이며, itemList.lastIndex는 리스트의 마지막 요소의 인덱스입니다 |
||
| editText.text.clear() | ||
| } | ||
| } | ||
| } | ||
| } | ||
| 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) | ||
|
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. 몇가지 질문이 있습니다
Author
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. 그 작업을 어느 activity에서 할건지 알려주는 것이라고 생각합니다 |
||
| .setTitle(R.string.dialog_title) | ||
| .setMessage(R.string.dialog_message) | ||
| .setPositiveButton(R.string.dialog_positive_message) { dialog, _ -> | ||
| items.removeAt(position) | ||
| notifyItemRemoved(position) | ||
|
Collaborator
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. 질문이 많으니 저도 질문
Author
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. item,removeAt(position)으로 리스트에서 제거는 되지만 내용이 갱신되지 않아 오류가 발생합니다 |
||
| dialog.dismiss() | ||
| } | ||
| .setNegativeButton(R.string.dialog_negative_message, null) | ||
| .show() | ||
| } | ||
| } | ||
|
|
||
| override fun getItemCount() = items.size | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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" | ||
|
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. naming convention 잘 지키셨네요! |
||
| 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> | ||
| 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> |
| 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> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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" | ||
|
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. 여기도 불필요해보입니다 |
||
|
|
||
| [libraries] | ||
| androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" } | ||
|
|
||
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.
불필요한 라이브러리 같은데, 추가하신 이유가 있을까요?
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.
처음에 fragment를 이용하여 풀어보려 했는데 결국엔 사용하지 않았습니다. 그 과정에서 미처 지우지 못했습니다