Home Android - Initialize variable with currently set preference
Reply: 3

Android - Initialize variable with currently set preference

Brandon Nolet
1#
Brandon Nolet Published in 2018-01-12 22:08:36Z

private int mShuffleMode = SharedPreferences.getInt("shufflemode");

Tells me that a Non-static method can't be referenced from a static context.

I'm not entirely sure what this means.

What I'm trying to do is initialise the variable with the preference that was set before.

CommonsWare
2#
CommonsWare Reply to 2018-01-12 22:13:23Z

getInt() is not a method that you call on the class SharedPreferences. Rather, you call it on an instance of SharedPreferences.

What I'm trying to do is initialise the variable with the preference that was set before.

Retrieve your SharedPreferences object, then call getInt() on that. If you have code that is saving SharedPreferences, you should already have access to that SharedPreferences object, or at least have code for retrieving it.

See the documentation on SharedPreferences for more.

Napster
3#
Napster Reply to 2018-01-12 22:14:16Z

You can't get data in a static way using SharedPreferences.getInt("shufflemode");

What you need to do is create an object of SharedPreference and then query it using SharedPreference sharedPreference = context.getSharedPreferences("name-of-preference",MODE) and then `sharedPreference.getInt("shufflemode")

Consider this link

Amine Choukri
4#
Amine Choukri Reply to 2018-01-12 22:14:56Z
private SharedPreferences sharedPref ;
private int mShuffleMode;

in your onCreate

sharedPref= context.getSharedPreferences("preferences_file_key", Context.MODE_PRIVATE);
mShuffleMode = sharedPref.getInt("shufflemode");
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.305228 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO