-
Notifications
You must be signed in to change notification settings - Fork 0
Assignment06 #3
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: 실습4
Are you sure you want to change the base?
Assignment06 #3
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 |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| #Tue Nov 26 00:44:34 KST 2024 | ||
| java.home=C\:\\Program Files\\Android\\Android Studio1\\jbr |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 |
|---|---|---|
| @@ -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)} | ||
| 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("") | ||
|
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. 커스텀 다이얼로그 활용해보시면 좋을 것 같습니다! |
||
| } | ||
|
|
||
| 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() | ||
| } | ||
| } | ||
| } | ||
| 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) | ||
| } | ||
|
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. NameViewHolder에서 ui 로직 처리하는 걸 권장드려요! |
||
| } | ||
|
|
||
| override fun getItemCount(): Int = nameList.size | ||
|
|
||
| class NameViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { | ||
| val name_text: TextView = itemView.findViewById(R.id.name_text) | ||
| } | ||
| } | ||
This file was deleted.
| 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> |
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.
네이밍컨벤션 통일해주세요!