Reputation: 345
This is need for DarkMode
I need to load my colorMode var from SharedPreferences in RecyclerView.Adapter, but IDE marks "this" or "application" like an error
where can I insert those code without any errors
val preferences = *this*.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE)
val editor = preferences.edit()
var colorMode = false
colorMode = preferences.getBoolean("ThemeMode", false)
Upvotes: 2
Views: 2861
Reputation: 2911
this
refers to the adapter and not the the context
.
You should call getSharedPreferences()
on the context
of your app.
If this code is inside the ViewHolder
, use this:
val preferences = view.getContext().getSharedPreferences("MyPreferences", Context.MODE_PRIVATE)
Best
Upvotes: 3