首页 > 解决方案 > 如何在 ViewModel 中动态更改属性

问题描述

所以在我的 DAO 中,我有一个这样的查询

@Query("Select * from transaction_history where wallet_type=:walletType AND email=:email AND transaction_type='Expenses' AND date BETWEEN :from AND :to")
fun getLastSevenDaysExpensesTransaction(walletType: String, email:String, from: Long?, to: Long?) : LiveData<List<ExpensesTransaction>>?

@Query("Select * from transaction_history where wallet_type=:walletType AND email=:email AND transaction_type='Income' AND date BETWEEN :from AND :to")
fun getLastSevenIncomeTransaction(walletType: String, email:String, from: Long?, to: Long?) : LiveData<List<IncomeTransaction>>

在我的 viewModel 中,这就是我调用 getLastSevenDaysExpensesTransaction 方法的方式

class GraphViewModel(dataSource: NetWalletDatabaseDao, application: Application, email: String, walletType: String) : ViewModel() {

val database = dataSource

var from : Long = 0L

var to : Long = 0L

val lastSevenDaysIncome = database.getLastSevenIncomeTransaction(walletType, email, from, to)

val lastSevenDaysExpenses = database.getLastSevenDaysExpensesTransaction(walletType, email, from, to)

fun funcLastSevenDaysIncome(fromParam: Long, toParam: Long) {
    from = fromParam
    to = toParam
}

在我的片段中,这就是我显示数据的方式

val application = requireNotNull(this.activity).application
    val dataSource = NetWalletDatabase.getInstance(application).netWalletDatabaseDao
    val viewModelFactory = GraphViewModelFactory(
        dataSource,
        application,
        getEmail.toString(),
        getWalletType.toString(),
    )
    val viewModel =
        ViewModelProvider(this, viewModelFactory).get(GraphViewModel::class.java)


    val tvLastSevenDays = binding.tvLastSevenDays
    viewModel.funcLastSevenDaysIncome(sevenDaysInMili, todayMili)

    val chart : LineChart = binding.chart
    val expenses = ArrayList<Entry>()
    viewModel.lastSevenDaysMut.observe(viewLifecycleOwner, Observer { list ->
        list?.let {
            for (i in 0..list.size - 1) {
                expenses.add(Entry(i.toFloat(), list.get(i).value!!.toFloat()))

            }
            Log.e("Result", list.get(0).value!!.toString())
        }
        val expensesLineDataSet = LineDataSet(expenses, "Expenses")
        expensesLineDataSet.mode = LineDataSet.Mode.CUBIC_BEZIER
        expensesLineDataSet.color = Color.BLUE
        expensesLineDataSet.circleRadius = 5f
        expensesLineDataSet.setCircleColor(Color.BLUE)

        val income = ArrayList<Entry>()
        viewModel.lastSevenDaysMut.observe(viewLifecycleOwner, Observer { list ->
            list?.let {
                for (i in 0..list.size - 1) {
                    income.add(Entry(i.toFloat(), list.get(i).value!!.toFloat()))

                    Log.e("Result", list.get(0).value!!.toString())

                }
            }
            val incomeLineDataSet = LineDataSet(income, "Income")
            incomeLineDataSet.mode = LineDataSet.Mode.CUBIC_BEZIER
            incomeLineDataSet.color = Color.RED
            incomeLineDataSet.circleRadius = 5f
            incomeLineDataSet.setCircleColor(Color.RED)

            val legend = chart.legend
            legend.isEnabled = true
            legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP)
            legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER)
            legend.setOrientation(Legend.LegendOrientation.HORIZONTAL)
            legend.setDrawInside(false)

            chart.description.isEnabled = false
            chart.xAxis.position = XAxis.XAxisPosition.BOTTOM
            chart.data = LineData(expensesLineDataSet, incomeLineDataSet)
            chart.animateXY(100, 500)
        })
    })

问题是数据不会显示。到目前为止,我所理解的为什么数据不会显示是,当我的 viewModel 运行时,即使在我的 Fragment 中,我的属性val lastSevenDaysIncome = database.getLastSevenIncomeTransaction(walletType, email, from, to)仍然为零,我已经调用了更改这些属性的方法。val lastSevenDaysExpenses = database.getLastSevenDaysExpensesTransaction(walletType, email, from, to)var from : Long = 0Lvar to : Long = 0LviewModel.funcLastSevenDaysIncome(sevenDaysInMili, todayMili)

那么我该如何解决呢?如何更改属性var from : Long = 0L以及var to : Long = 0L在它们用于查询之前?

PS我曾经将查询所需的所有参数放在构造函数中。例如

class GraphViewModel(dataSource: NetWalletDatabaseDao, application: Application, email: String, walletType: String, from: Long, to: Long) : ViewModel()

并使用它例如

val lastSevenDaysIncome = database.getLastSevenIncomeTransaction(walletType, email, from, to)

但是,由于“from”和“to”是动态的,我不能再使用它了,因为在我的片段中,我必须像这样初始化 viewModelProvider

val viewModelFactory = GraphViewModelFactory(
    dataSource,
    application,
    getEmail.toString(),
    getWalletType.toString(),
***dynamic from*
*dynamic to***
)

先感谢您。

标签: androidkotlinmvvmandroid-room

解决方案


推荐阅读