Unresolved reference getValue() in kotlin (Android Studio 3)
up vote
0
down vote
favorite
I have the following piece of code for a setting page using Preferences. I can find the correct ListPreference, I can set the summary string, but I cannot get the value from the preference object. At least Android Studio cannot find the reference to the getValue() method.
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.preference.ListPreference
import android.support.v7.preference.Preference
import android.support.v7.preference.PreferenceFragmentCompat
class SettingsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_settings)
setSupportActionBar(findViewById(R.id.settings_toolbar))
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportFragmentManager
.beginTransaction()
.replace(R.id.content_settings, MySettingsFragment())
.commit()
}
class MySettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.pref_settings, rootKey)
val pref = findPreference("selected_maptype")
pref.summary = "This is a summary"
val current_value = pref.getValue()
}
}
}
android kotlin android-preferences
add a comment |
up vote
0
down vote
favorite
I have the following piece of code for a setting page using Preferences. I can find the correct ListPreference, I can set the summary string, but I cannot get the value from the preference object. At least Android Studio cannot find the reference to the getValue() method.
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.preference.ListPreference
import android.support.v7.preference.Preference
import android.support.v7.preference.PreferenceFragmentCompat
class SettingsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_settings)
setSupportActionBar(findViewById(R.id.settings_toolbar))
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportFragmentManager
.beginTransaction()
.replace(R.id.content_settings, MySettingsFragment())
.commit()
}
class MySettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.pref_settings, rootKey)
val pref = findPreference("selected_maptype")
pref.summary = "This is a summary"
val current_value = pref.getValue()
}
}
}
android kotlin android-preferences
Have you tried.value
?
– EpicPandaForce
Nov 13 at 11:47
1
I don't see getValue() here... developer.android.com/reference/android/preference/Preference
– ZUNJAE
Nov 13 at 11:49
shouldn't you be calling likepref.getDataType()
?
– Samuel Robert
Nov 13 at 11:49
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have the following piece of code for a setting page using Preferences. I can find the correct ListPreference, I can set the summary string, but I cannot get the value from the preference object. At least Android Studio cannot find the reference to the getValue() method.
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.preference.ListPreference
import android.support.v7.preference.Preference
import android.support.v7.preference.PreferenceFragmentCompat
class SettingsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_settings)
setSupportActionBar(findViewById(R.id.settings_toolbar))
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportFragmentManager
.beginTransaction()
.replace(R.id.content_settings, MySettingsFragment())
.commit()
}
class MySettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.pref_settings, rootKey)
val pref = findPreference("selected_maptype")
pref.summary = "This is a summary"
val current_value = pref.getValue()
}
}
}
android kotlin android-preferences
I have the following piece of code for a setting page using Preferences. I can find the correct ListPreference, I can set the summary string, but I cannot get the value from the preference object. At least Android Studio cannot find the reference to the getValue() method.
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.preference.ListPreference
import android.support.v7.preference.Preference
import android.support.v7.preference.PreferenceFragmentCompat
class SettingsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_settings)
setSupportActionBar(findViewById(R.id.settings_toolbar))
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportFragmentManager
.beginTransaction()
.replace(R.id.content_settings, MySettingsFragment())
.commit()
}
class MySettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.pref_settings, rootKey)
val pref = findPreference("selected_maptype")
pref.summary = "This is a summary"
val current_value = pref.getValue()
}
}
}
android kotlin android-preferences
android kotlin android-preferences
asked Nov 13 at 11:45
HWK
243
243
Have you tried.value
?
– EpicPandaForce
Nov 13 at 11:47
1
I don't see getValue() here... developer.android.com/reference/android/preference/Preference
– ZUNJAE
Nov 13 at 11:49
shouldn't you be calling likepref.getDataType()
?
– Samuel Robert
Nov 13 at 11:49
add a comment |
Have you tried.value
?
– EpicPandaForce
Nov 13 at 11:47
1
I don't see getValue() here... developer.android.com/reference/android/preference/Preference
– ZUNJAE
Nov 13 at 11:49
shouldn't you be calling likepref.getDataType()
?
– Samuel Robert
Nov 13 at 11:49
Have you tried
.value
?– EpicPandaForce
Nov 13 at 11:47
Have you tried
.value
?– EpicPandaForce
Nov 13 at 11:47
1
1
I don't see getValue() here... developer.android.com/reference/android/preference/Preference
– ZUNJAE
Nov 13 at 11:49
I don't see getValue() here... developer.android.com/reference/android/preference/Preference
– ZUNJAE
Nov 13 at 11:49
shouldn't you be calling like
pref.getDataType()
?– Samuel Robert
Nov 13 at 11:49
shouldn't you be calling like
pref.getDataType()
?– Samuel Robert
Nov 13 at 11:49
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
getValue()
is method of ListPreference not Preference, so you need to cast it.
val pref = findPreference("selected_maptype") as ListPreference
val current_value = pref.value
You will use pref.value
instead of pref.getValue()
in Kotlin. Kotlin changes getters setters in direct name.
Thanks! New to Kotlin, so still finding out some of the basics...
– HWK
Nov 13 at 12:05
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
getValue()
is method of ListPreference not Preference, so you need to cast it.
val pref = findPreference("selected_maptype") as ListPreference
val current_value = pref.value
You will use pref.value
instead of pref.getValue()
in Kotlin. Kotlin changes getters setters in direct name.
Thanks! New to Kotlin, so still finding out some of the basics...
– HWK
Nov 13 at 12:05
add a comment |
up vote
0
down vote
getValue()
is method of ListPreference not Preference, so you need to cast it.
val pref = findPreference("selected_maptype") as ListPreference
val current_value = pref.value
You will use pref.value
instead of pref.getValue()
in Kotlin. Kotlin changes getters setters in direct name.
Thanks! New to Kotlin, so still finding out some of the basics...
– HWK
Nov 13 at 12:05
add a comment |
up vote
0
down vote
up vote
0
down vote
getValue()
is method of ListPreference not Preference, so you need to cast it.
val pref = findPreference("selected_maptype") as ListPreference
val current_value = pref.value
You will use pref.value
instead of pref.getValue()
in Kotlin. Kotlin changes getters setters in direct name.
getValue()
is method of ListPreference not Preference, so you need to cast it.
val pref = findPreference("selected_maptype") as ListPreference
val current_value = pref.value
You will use pref.value
instead of pref.getValue()
in Kotlin. Kotlin changes getters setters in direct name.
answered Nov 13 at 11:53
Khemraj
10.4k22463
10.4k22463
Thanks! New to Kotlin, so still finding out some of the basics...
– HWK
Nov 13 at 12:05
add a comment |
Thanks! New to Kotlin, so still finding out some of the basics...
– HWK
Nov 13 at 12:05
Thanks! New to Kotlin, so still finding out some of the basics...
– HWK
Nov 13 at 12:05
Thanks! New to Kotlin, so still finding out some of the basics...
– HWK
Nov 13 at 12:05
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280352%2funresolved-reference-getvalue-in-kotlin-android-studio-3%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Have you tried
.value
?– EpicPandaForce
Nov 13 at 11:47
1
I don't see getValue() here... developer.android.com/reference/android/preference/Preference
– ZUNJAE
Nov 13 at 11:49
shouldn't you be calling like
pref.getDataType()
?– Samuel Robert
Nov 13 at 11:49