正在从另一个活动检索共享首选项项目.安卓工作室,Kotlin,API 29



我是一个写第一个应用程序的新手。我创建了一个名为SettingsInput.kt的"活动",它可以很好地将设置保存为sharedPreferences,并在按下按钮时显示它们。

现在,我想在函数中使用其中一个设置作为计算的一部分,从数据库中的几个表中挑选旧数据。该函数位于databaseHandler类中。

我得到一个错误";未解析的引用:getSharedPreferences"到目前为止,我尝试过的方法都没有帮助。考虑到API的目标水平是API 29,有人能帮忙吗?

var sharedPrefFile = "greenbandbasicpreference"
val sharedPreferences:SharedPreferences = getSharedPreferences(sharedPrefFile,Context.MODE_PRIVATE)//doesnt recognise getSharedPreferences
fun cullData(){
var noow = ZonedDateTime.now(Clock.systemUTC())
var noowSecs:Long  = noow.toEpochSecond()
var noowMins:Long  = (noowSecs)/60
//var bolusLifeMins:Long = 220// this has to come from a database or store of "preferences"
//var bolusLifeMins:Long = sharedPreferences.getLong("insLife_key",0)
val bolusLifeMins   = getSharedPreferences("greenbandbasicpreference",Context.MODE_PRIVATE)
var minTimeMins = noowMins - bolusLifeMins - 2*(24*60)
val db = this.writableDatabase
db.delete(
"BolusTable",
"btime <"+ minTimeMins ,
null
)
db.delete(//this is a part of the function for cull CarbsTable
"CarbsTable",
"carbTime <"+ minTimeMins ,
null
)

db.close()
}

方法getSharedPreferences是Context类中的一个方法,因此您需要一个上下文来请求它,例如:

context.getSharedPreferences(sharedPrefFile,Context.MODE_PRIVATE)

https://developer.android.com/reference/android/content/Context#getSharedPreferences(java.lang.String,%20int(

最新更新