-1

I am currently making a Loan Calculator that uses ViewModel to store data between fragments. The app uses EditText and Seekbar user can plot in data and sends it to a shared Viewmodel.

When I try to convert string input from user, I struggle to convert the data into Float value so I can use it to calculate the loan plan.

Do anyone know how to do this?

Input LoanNumber from Edit Text: AnnuitetViewModel.kt

 private var _loanNumber = MutableLiveData<Float>()
    val loanNumber: LiveData<Float>
        get() = _loanNumber

    fun saveLoanNumber(newLoanNumber: Float){
        _loanNumber.value = newLoanNumber
    }

AnnuitetFrament.kt

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    binding.lifecycleOwner = this
    viewModel = ViewModelProvider(this)[AnnuitetViewModel::class.java]
    binding.viewmodel = viewModel


    AnnuitetViewModel.loanNumber.observe(viewLifecycleOwner){
        binding.laanebelop.text
    }

 }

    binding.BeregnLaanAnnuitet.setOnClickListener {
        AnnuitetViewModel.saveLoanNumber(binding.laanebelop.text.toString())
      
        findNavController().navigate(R.id.action_annuitetFragment_to_annuitetPlanFragment)
    }

fragment:annuitet.xml:

<EditText
                    android:id="@+id/laanebelop"
                    android:layout_width="196dp"
                    android:layout_height="56dp"
                    android:autofillHints=""
                    android:ems="10"
                    android:hint="@string/hint"
                    android:inputType="numberDecimal"
                    android:text=""
                    android:textColorHint="#757575"
                    android:visibility="visible"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.73"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintVertical_bias="0.248" />
  • If I use "@{string/right_format_resource(viewmodel.floatValue)}" and Context.getString(R.string.right_format_resource, floatValue). Can then store the data as float in MutableLiveData()? – Nicholai Mørch Rindarøy Mar 17 '22 at 14:21
  • you need to observe then parse where is the problem ? ... if you wana get value as it is **AT GIVEN TIME** (so it takes value when it executuse doesn't aware of chages) you need to use `LiveData.getValue()` (it can be null) then parse – Selvin Mar 17 '22 at 14:31
  • Manage to transform _loanNumber to float. Then I can start calculation the loan – Nicholai Mørch Rindarøy Mar 17 '22 at 15:31

1 Answers1

0

Solve it. By using Transformation.map I could change String to Float. Then I can move on to calculate the loan plan.

private var _loanNumber = MutableLiveData<String>()
val loanNumber: LiveData<Float>
     get() = Transformations.map(_loanNumber) { it.toFloat() }