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 .gradle/config.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#Tue Nov 26 00:44:34 KST 2024
java.home=C\:\\Program Files\\Android\\Android Studio1\\jbr
1 change: 1 addition & 0 deletions .idea/.name

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

184 changes: 184 additions & 0 deletions .idea/workspace.xml

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

8 changes: 4 additions & 4 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {

android {
namespace = "com.example.assignment02"
compileSdk = 34
compileSdk = 35

defaultConfig {
applicationId = "com.example.assignment02"
Expand Down Expand Up @@ -36,11 +36,11 @@ android {
}

dependencies {
implementation("androidx.activity:activity-ktx:1.7.0")
implementation("androidx.core:core-ktx:1.13.1")

implementation("androidx.core:core-ktx:1.15.0")
implementation("androidx.appcompat:appcompat:1.7.0")
implementation("com.google.android.material:material:1.12.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("androidx.constraintlayout:constraintlayout:2.2.0")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.2.1")
androidTestImplementation("androidx.test.espresso:espresso-core:3.6.1")
Expand Down
1 change: 0 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".RandomActivity" />
</application>

</manifest>
89 changes: 60 additions & 29 deletions app/src/main/java/com/example/assignment02/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,42 +1,73 @@
package com.example.assignment02

import android.app.Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class MainActivity : AppCompatActivity() {

val nameList = mutableListOf<String>()

lateinit var name_text: EditText
lateinit var recycler_view: RecyclerView
lateinit var nameAdapter: NameAdapter
lateinit var add_button: Button

override fun onCreate(savedInstanceState: Bundle?) {
// 포크용 변경
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val number_text: TextView = findViewById(R.id.number_text)
val count_btn: TextView = findViewById(R.id.count_btn)
val toast_btn: TextView = findViewById(R.id.toast_btn)
val random_btn: TextView = findViewById(R.id.random_btn)
val intent = Intent(this, RandomActivity::class.java)

val resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
number_text.text = result.data?.getIntExtra("random_number", 0).toString()
Toast.makeText(this, "OK", Toast.LENGTH_SHORT).show()

name_text = findViewById(R.id.name_text)
recycler_view = findViewById(R.id.recycler_view)
nameAdapter = NameAdapter(nameList) { position ->showDeleteDialog(position)}

Choose a reason for hiding this comment

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

네이밍컨벤션 통일해주세요!

add_button = findViewById(R.id.add_button)

recycler_view.layoutManager = LinearLayoutManager(this)
recycler_view.adapter = nameAdapter

add_button.setOnClickListener {
nameList.add(name_text.text.toString())
nameAdapter.notifyItemInserted(nameList.size - 1)
name_text.setText("")
}
}

private fun showDeleteDialog(position: Int) {
AlertDialog.Builder(this)
.setTitle("이름 목록 삭제하기")
.setMessage("이름 목록을 삭제해보자.")
.setPositiveButton("확인") { dialog, _ ->
showEditDialog(position)
dialog.dismiss()
}
else {
Toast.makeText(this, "오류", Toast.LENGTH_SHORT).show()
.setNegativeButton("취소") { dialog, _ ->
dialog.dismiss()
}
.create()
.show()
}
private fun showEditDialog(position: Int) {
val editText = EditText(this).apply {
hint = "수정할 이름을 입력하세요."
setText("")

Choose a reason for hiding this comment

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

커스텀 다이얼로그 활용해보시면 좋을 것 같습니다!

}

count_btn.setOnClickListener {
number_text.text = (number_text.text.toString().toInt()+1).toString()
}
toast_btn.setOnClickListener {
Toast.makeText(this, "숫자 : "+number_text.text, Toast.LENGTH_LONG).show()
}
random_btn.setOnClickListener {
intent.putExtra("number", number_text.text.toString().toInt())
resultLauncher.launch(intent)
}
AlertDialog.Builder(this)
.setTitle(" ")
.setView(editText)
.setPositiveButton("확인") { _, _ ->
nameList[position] = editText.text.toString()
nameAdapter.notifyItemChanged(position)
}
.setNegativeButton("취소") { dialog, _ ->
dialog.dismiss()
}
.create()
.show()
}
}
}
32 changes: 32 additions & 0 deletions app/src/main/java/com/example/assignment02/NameAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.example.assignment02

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 nameList: MutableList<String>,
private val onItemClick: (Int) -> Unit
) : RecyclerView.Adapter<NameAdapter.NameViewHolder>() {

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

override fun onBindViewHolder(holder: NameViewHolder, position: Int) {
holder.name_text.text = nameList[position]
holder.itemView.setOnClickListener {
onItemClick(position)
}

Choose a reason for hiding this comment

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

NameViewHolder에서 ui 로직 처리하는 걸 권장드려요!
ui가 복잡해지면 성능이 저하될 수 있습니다.

}

override fun getItemCount(): Int = nameList.size

class NameViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val name_text: TextView = itemView.findViewById(R.id.name_text)
}
}
28 changes: 0 additions & 28 deletions app/src/main/java/com/example/assignment02/RandomActivity.kt

This file was deleted.

5 changes: 5 additions & 0 deletions app/src/main/res/drawable/round_button.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/holo_orange_dark" />
<corners android:radius="10dp" />
</shape>
Loading