0

My app contains shared preferences, so in my "Settings-Menu" I can select an item from a list.

In another java file I want to access the value of the selected item.

I failed passing the integer-value through an intent, because the integer is declared in private static, private boolean, and Android Studio tells me, it cannot be refferenced from a static content.

So how can I receive my value from this integer? Thanks in advance :).



Edit: firstival, thanks a lot for that many answers! But as I'm not that much into java I wasn't able to follow your instructions, so I'm going to depict my problem closer. I'm using androids template "Settings-Activity". In my strings.xml I've got

 <string name="Values">Values</string>
        <string-array name="pref_example_list_titles">
            <item>A</item>
            <item>B</item>
            <item>C</item>
        </string-array>

        <string-array name="pref_example_list_values">
            <item>01</item>
            <item>02</item>
            <item>03</item>
        </string-array>

In my SettingsActivity.java

private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {

        @Override
        public boolean onPreferenceChange(Preference preference, Object value) {
            String stringValue = value.toString();

            if (preference instanceof ListPreference) {

                ListPreference listPreference = (ListPreference) preference;
                int index = listPreference.findIndexOfValue(stringValue);


                preference.setSummary(
                        index >= 0
                                ? listPreference.getEntries()[index]
                                : null);
{...}

In another java file, a fragment "FirstFragment.java I finally want to assign the value from pref_example_list_values to a new integer, called "Vallue2".

So could you please explain me, how I do this, bc I couldn't follow your explainations. Thanks a lot!

Quacksilber
  • 163
  • 1
  • 14
  • take a look at : http://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values. If still unclear post you code – Ahmed Abidi Dec 14 '16 at 11:27
  • post the code you are using to pass the datas through activities please – Pier Giorgio Misley Dec 14 '16 at 11:31
  • Actually I don't have any code to pass the datas through the activities, thats why I'm asking for it. – Quacksilber Dec 14 '16 at 11:33
  • And by using: SharedPreferences prefs = this.getSharedPreferences( "com.example.app", Context.MODE_PRIVATE); I get the error that "getsharedpreferences" cannot be ressolved – Quacksilber Dec 14 '16 at 11:35
  • Please, read [this (how to ask)](http://stackoverflow.com/help/how-to-ask) and [this (mcve)](http://stackoverflow.com/help/mcve) before asking, as those will help you get more and better answers from the community. – Bonatti Dec 14 '16 at 11:56
  • Try to call another Activity from non static method eg: private static int value=1; public void start(){ Intent intent = new Intent(this,SecondActivity.class); intent.putExtra("value",value); startActivity(intent); } – saeed Dec 14 '16 at 12:00

1 Answers1

0

As per documentation:

A SharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write them.

This means that a single file holds the value, and the preffered way to read and write to it are as following:

SharedPreferences preference = yourContext.getSharedPreferences(yourFileName), Context.MODE_PRIVATE
);
preference.edit().putString(AN_IDENTIFIER, "aValue").apply();

Then to retrieve the value:

SharedPreferences preference = yourContext.getSharedPreferences(yourFileName), Context.MODE_PRIVATE
);
preference.getString(AN_IDENTIFIER,"a default value to use");
Bonatti
  • 2,778
  • 5
  • 23
  • 42
xbadal
  • 1,284
  • 2
  • 11
  • 24
  • When answering a question, answer it. Do not link the answer elsewhere, also note that your link is broken. (404 page). – Bonatti Dec 14 '16 at 11:56
  • May i suggest some improvements? I will edit your answer, and if you dislike it, I will undo it. – Bonatti Dec 14 '16 at 12:04
  • yeah sure! go on ! – xbadal Dec 14 '16 at 12:06
  • Just like questions have guidelines, so do [answers](http://stackoverflow.com/help/how-to-answer). Try to write good ones, and the upvotes will come, I do not think your answer is worth up/down votes, you likely had people see it, and as it did not answer it, voted down. Be patient, write only when your words can help, and then, when you have enough reputation, use the `comments` to write suggestions or links, or request for further details. – Bonatti Dec 14 '16 at 12:20
  • Thats Okay! i understand. as i am new and learning things. Thanks for you suggestion. @Bonatti – xbadal Dec 14 '16 at 12:25