-
Notifications
You must be signed in to change notification settings - Fork 12
[김상진_Android] 12주차 과제 제출 #84
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
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.
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.
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.
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.
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 |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package com.example.bcsd_android_2025_1 | ||
|
|
||
| import android.app.Activity | ||
| import android.content.Intent | ||
| import android.os.Bundle | ||
| import androidx.activity.viewModels | ||
| import androidx.appcompat.app.AppCompatActivity | ||
| import com.example.bcsd_android_2025_1.databinding.ActivityAddEditBinding | ||
|
|
||
|
|
||
| class AddEditActivity : AppCompatActivity(){ | ||
| private lateinit var binding: ActivityAddEditBinding | ||
| private val viewModel:WordViewModel by viewModels() | ||
|
|
||
| private var wordId: Int?= null | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
|
|
||
| super.onCreate(savedInstanceState) | ||
| binding = ActivityAddEditBinding.inflate(layoutInflater) | ||
| setContentView(binding.root) | ||
|
|
||
| val editWordText = intent.getStringExtra("word_text") | ||
| val editMeaning = intent.getStringExtra("word_meaning") | ||
| wordId = intent.getIntExtra("word_id", -1).takeIf { it != -1 } | ||
|
|
||
| binding.wordEdittext.setText(editWordText) | ||
| binding.meaningEdittext.setText(editMeaning) | ||
|
|
||
| binding.addButton.setOnClickListener { | ||
| val wordText = binding.wordEdittext.text.toString() | ||
| val meaning = binding.meaningEdittext.text.toString() | ||
| if (wordText.isNotBlank() && meaning.isNotBlank()) { | ||
| val word = WordListData(wordId ?: 0, wordText, meaning) | ||
| if (wordId != null) { | ||
| viewModel.update(word) | ||
| binding.wordEdittext.setText("") | ||
| binding.meaningEdittext.setText("") | ||
| val resultIntent = Intent() | ||
| resultIntent.putExtra("edited_word", word.word) | ||
| resultIntent.putExtra("edited_meaning", word.meaning) | ||
| setResult(Activity.RESULT_OK, resultIntent) | ||
| finish() | ||
|
|
||
| } else { | ||
| viewModel.insert(word) | ||
| } | ||
| finish() | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,78 @@ | ||
| package com.example.bcsd_android_2025_1 | ||
|
|
||
| import android.app.Activity | ||
| import android.content.Intent | ||
| import android.os.Bundle | ||
| import androidx.activity.enableEdgeToEdge | ||
| import androidx.activity.result.ActivityResultLauncher | ||
| import androidx.activity.result.contract.ActivityResultContracts | ||
| import androidx.activity.viewModels | ||
| import androidx.appcompat.app.AppCompatActivity | ||
| import androidx.core.view.ViewCompat | ||
| import androidx.core.view.WindowInsetsCompat | ||
| import androidx.recyclerview.widget.LinearLayoutManager | ||
| import com.example.bcsd_android_2025_1.databinding.ActivityMainBinding | ||
|
|
||
|
|
||
| class MainActivity : AppCompatActivity() { | ||
|
|
||
| private lateinit var binding: ActivityMainBinding | ||
| private val viewModel: WordViewModel by viewModels() | ||
| private lateinit var editActivityLauncher: ActivityResultLauncher<Intent> | ||
|
|
||
|
|
||
| private val adapter by lazy { | ||
| WordAdapter(onTopClick = { viewModel.setTopWord(it) }) | ||
| } | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| setContentView(R.layout.activity_main) | ||
| binding = ActivityMainBinding.inflate(layoutInflater) | ||
| setContentView(binding.root) | ||
|
|
||
| editActivityLauncher = | ||
| registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> | ||
| if (result.resultCode == Activity.RESULT_OK) { | ||
| val data = result.data | ||
| val editedWord = data?.getStringExtra("edited_word") ?: "" | ||
| val editedMeaning = data?.getStringExtra("edited_meaning") ?: "" | ||
|
|
||
| // 상단 TextView 업데이트 | ||
| binding.wordTextview.text = editedWord | ||
| binding.meaningTextview.text = editedMeaning | ||
| } | ||
| } | ||
|
|
||
| binding.recyclerView.adapter = adapter | ||
| binding.recyclerView.layoutManager = LinearLayoutManager(this) | ||
|
|
||
| binding.editButton.setOnClickListener { | ||
| viewModel.topWord.value?.let { | ||
| val intent = Intent(this, AddEditActivity::class.java).apply { | ||
| putExtra("word_id", it.id) | ||
| putExtra("word_text", it.word) | ||
| putExtra("word_meaning", it.meaning) | ||
| } | ||
| editActivityLauncher.launch(intent) | ||
| } | ||
| } | ||
|
|
||
| binding.deleteButton.setOnClickListener { | ||
| viewModel.topWord.value?.let { | ||
| viewModel.delete(it) | ||
| binding.wordTextview.text = "" | ||
| binding.meaningTextview.text = "" | ||
| } | ||
| } | ||
|
|
||
| viewModel.allWords.observe(this) { | ||
| adapter.submitList(it) | ||
| } | ||
|
|
||
| viewModel.topWord.observe(this) { | ||
| binding.wordTextview.text = it?.word ?: "단어 선택 필요" | ||
|
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. strings.xml |
||
| binding.meaningTextview.text = it?.meaning ?: "" | ||
| } | ||
|
|
||
| binding.floatingButton.setOnClickListener { | ||
| startActivity(Intent(this, AddEditActivity::class.java)) | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package com.example.bcsd_android_2025_1 | ||
|
|
||
| import android.view.LayoutInflater | ||
| import android.view.ViewGroup | ||
| import androidx.recyclerview.widget.DiffUtil | ||
| import androidx.recyclerview.widget.ListAdapter | ||
| import androidx.recyclerview.widget.RecyclerView | ||
| import com.example.bcsd_android_2025_1.databinding.ItemWordBinding | ||
|
|
||
| class WordAdapter ( | ||
| private val onTopClick: (WordListData) ->Unit | ||
| ) : ListAdapter<WordListData, WordAdapter.WordViewHolder>(DiffCallback()){ | ||
|
|
||
| inner class WordViewHolder(private val binding: ItemWordBinding): RecyclerView.ViewHolder(binding.root) { | ||
| fun bind(word: WordListData) { | ||
| binding.word = word | ||
| binding.root.setOnClickListener { onTopClick(word) } | ||
| binding.executePendingBindings() | ||
| } | ||
| } | ||
|
|
||
| override fun onCreateViewHolder(parent:ViewGroup, viewType: Int):WordViewHolder{ | ||
| val binding = ItemWordBinding.inflate(LayoutInflater.from(parent.context), parent, false) | ||
| return WordViewHolder(binding) | ||
| } | ||
|
|
||
| override fun onBindViewHolder(holder: WordViewHolder, position:Int){ | ||
| holder.bind(getItem(position)) | ||
| } | ||
|
|
||
| class DiffCallback: DiffUtil.ItemCallback<WordListData>(){ | ||
| override fun areItemsTheSame(oldItem:WordListData, newItem:WordListData):Boolean = oldItem.id ==newItem.id | ||
| override fun areContentsTheSame(oldItem: WordListData, newItem: WordListData): Boolean = oldItem == newItem | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package com.example.bcsd_android_2025_1 | ||
|
|
||
| import androidx.lifecycle.LiveData | ||
| import androidx.room.* | ||
|
|
||
| @Dao | ||
| interface WordDao { | ||
| @Query("SELECT * FROM word_table ORDER BY id DESC") | ||
| fun getAllwords(): LiveData<List<WordListData>> | ||
|
|
||
| @Insert(onConflict = OnConflictStrategy.REPLACE) | ||
| suspend fun insert(word: WordListData) | ||
|
|
||
| @Update | ||
| suspend fun update(word: WordListData) | ||
|
|
||
| @Delete | ||
| suspend fun delete(wordListData: WordListData) | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package com.example.bcsd_android_2025_1 | ||
|
|
||
| import android.content.Context | ||
| import androidx.room.Room | ||
| import androidx.room.Database | ||
| import androidx.room.RoomDatabase | ||
|
|
||
| @Database(entities = [WordListData::class], version = 1, exportSchema = false) | ||
| abstract class WordDatabase : RoomDatabase() { | ||
| abstract fun wordDao(): WordDao | ||
|
|
||
| companion object{ | ||
| @Volatile private var INSTANCE: WordDatabase?=null | ||
|
|
||
| fun getDatabase(context: Context): WordDatabase{ | ||
| return INSTANCE ?: synchronized(this){ | ||
| val instance = Room.databaseBuilder( | ||
| context.applicationContext, WordDatabase::class.java, | ||
| "word_database" | ||
| ).build() | ||
| INSTANCE = instance | ||
| instance | ||
| } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package com.example.bcsd_android_2025_1 | ||
|
|
||
| import androidx.room.PrimaryKey | ||
| import androidx.room.Entity | ||
|
|
||
| @Entity(tableName = "word_table") | ||
| data class WordListData ( | ||
| @PrimaryKey(autoGenerate = true) | ||
| val id: Int = 0, | ||
| var word: String, | ||
| var meaning: String | ||
| ) |
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.
companion object 로 관리하면 좋을 거 같습니다.
다른 intent key 값들도 마찬가지입니다.