android - 如何在 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 = 0L
var to : Long = 0L
viewModel.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***
)
先感谢您。
解决方案
推荐阅读
- android - Android - SQLite 插入字节 [] 由于违反 NOT NULL 约束而失败
- node.js - Docker 服务未运行容器
- python - 将txt文件中的数据读入二维数组python
- sorting - 自定义比较器 - Java
- php - 在静态域名上使用 XAMPP 服务器托管我的网站
- ios - swreavealviewcontroller导航栏出口后黑屏
- jquery - jQuery 的 .html() 是否保留元素的数据?
- android - 字符串日期到毫秒
- ruby - 如何将方法添加到 FactoryBot 工厂?
- visual-studio-code - VSC 缺少调试和任务菜单