2021年4月24日星期六

Performing calculations based on multiple entries of data being fed from ViewModel

I'm having a bit of difficulty figuring out how to approach this function. I have an SQLite database that is being handled by room, and I need to update entries in sharedpreferences based on this data. There is already a dao query setup to give entries by descending order of datetime:

@Query("SELECT * FROM data_table ORDER BY datetime DESC")  LiveData<List<Data>> getAllData();  

These datetimes have corresponding float entries that I have to perform cumulative calculations on based on the difference of time to the next data entry's datetime. So, for example:

id     datetime     float  1      dt(1)        12.0f  2      dt(2)        15.0f  2      dt(3)        13.0f  

I would start with

var timeDiff = ((Duration.between(LocalDateTime.parse(dt(1)),LocalDateTime.parse(dt(2)).toMillis())/1000).toFloat()  var currentValue = [big complicated formula applied to 12.0f based on timeDiff]  

and then I would move on to

currentValue = (currentValue + 15.0f)  timeDiff = ((Duration.between(LocalDateTime.parse(dt(2)),LocalDateTime.parse(dt(3)).toMillis())/1000).toFloat()  currentValue = [big complicated formula applied to previous currentValue based on timeDiff]  

until I get to the last entry, where I store that datetime and currentValue as a sharedpreference.

I'm not really sure how to go about this though. I was thinking of using a mutable array list:

mDataViewModel = ViewModelProvider(this).get(DataViewModel::class.java)  var tarray: MutableList<List<Data?>?> = ArrayList()  mDataViewModel.getAllData().observe(this, Observer<List<Data>>() {     fun onChanged(data: List<Data?>?) {        tarray.add(data)     }  })          Toast.makeText(this@MainActivity, tarray.toString(), Toast.LENGTH_SHORT).show()  

and then performing the calculations on the list sequentially, but it doesn't appear to be populating. I would much prefer to perform the cumulative calculations as they're fed from the database. This is my first time using SQlite though, and I'm not there is a much easier way of accomplishing this than what I'm attempting to do. Any advice would be greatly appreciated.

https://stackoverflow.com/questions/67245449/performing-calculations-based-on-multiple-entries-of-data-being-fed-from-viewmod April 25, 2021 at 01:13AM

没有评论:

发表评论