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()
}
}
}









share|improve this question






















  • 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















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()
}
}
}









share|improve this question






















  • 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













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()
}
}
}









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 like pref.getDataType()?
    – Samuel Robert
    Nov 13 at 11:49


















  • 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
















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












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.






share|improve this answer





















  • Thanks! New to Kotlin, so still finding out some of the basics...
    – HWK
    Nov 13 at 12:05











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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.






share|improve this answer





















  • Thanks! New to Kotlin, so still finding out some of the basics...
    – HWK
    Nov 13 at 12:05















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.






share|improve this answer





















  • Thanks! New to Kotlin, so still finding out some of the basics...
    – HWK
    Nov 13 at 12:05













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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?