mutabledata를 가진 viewmodel 써보기 실습
참고-개발하는정대리
class ViewModelTutorialAcitivty : AppCompatActivity(), View.OnClickListener {
lateinit var myNumberViewModel: MyViewModel
private lateinit var binding: ActivityViewModelTutorialAcitivtyBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityViewModelTutorialAcitivtyBinding.inflate(layoutInflater)
setContentView(binding.root)
myNumberViewModel = ViewModelProvider(this)[MyViewModel::class.java]
myNumberViewModel.currentValue.observe(this, Observer {
Log.d(MyViewModel.TAG, "currentValue라이브 데이터 값 변경 :$it")
binding.numberTextview.text = it.toString()
})
binding.plusBtn.setOnClickListener(this)
binding.minusBtn.setOnClickListener(this)
}
override fun onClick(view: View?) {
val userInput = binding.numberInputEdittext.text.toString().toInt()
when (view) {
binding.plusBtn ->
myNumberViewModel.updateValue(ActionType.PLUS, userInput)
binding.minusBtn ->
myNumberViewModel.updateValue(ActionType.MINUS, userInput)
}
}
}
enum class ActionType {
PLUS, MINUS
}
class MyViewModel : ViewModel() {
companion object {
const val TAG: String = "로그"
}
private val _currentValue = MutableLiveData<Int>()
val currentValue: LiveData<Int>
get() = _currentValue
init {
Log.d(TAG, "생성자호출")
_currentValue.value = 0
}
fun updateValue(actionType: ActionType, input: Int) {
when (actionType) {
ActionType.PLUS -> _currentValue.value = _currentValue.value?.plus(input)
ActionType.MINUS -> _currentValue.value = _currentValue.value?.minus(input)
}
}
}
//ViewModelTutorialAcitivty.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingHorizontal="10dp"
tools:context=".ViewModelTutorialAcitivty">
<TextView
android:id="@+id/number_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/number_input_edittext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:inputType="number"
app:layout_constraintEnd_toStartOf="@+id/plus_btn"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/number_textview" />
<Button
android:id="@+id/plus_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:text="더하기"
app:layout_constraintEnd_toStartOf="@+id/minus_btn"
app:layout_constraintStart_toEndOf="@+id/number_input_edittext"
app:layout_constraintTop_toTopOf="@+id/number_input_edittext" />
<Button
android:id="@+id/minus_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="빼기"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/plus_btn"
app:layout_constraintTop_toTopOf="@+id/number_input_edittext" />
</androidx.constraintlayout.widget.ConstraintLayout>