getSharedPreferences - unable to get value


Last Updated:

  1. DroidHead

    DroidHead New Member This Topic's Starter

    Joined:
    Nov 21, 2009
    Messages:
    3
    Likes Received:
    0
    I've tried for a few hours over a two-day period and I am burnt, so if someone can help me figure out what I am doing wrong (or is this a known, unknown defect?) with regards to getting a value from ListPreference by way of SharedPreferences.

    Here are some code snippets to help you to see what I've done so far in my project:

    Main .java file:

    public static final String PREF_FILE_NAME = "preferences";

    seekback.setOnClickListener(new Button.OnClickListener() {
    @Override
    public void onClick(View view) {
    try {
    SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
    Integer storedPreference = preferences.getInt("seek", 0);

    //Toast.makeText(getBaseContext(), storedPreference + "", Toast.LENGTH_SHORT).show();
    mediaPlayer.seekTo(mediaPlayer.getCurrentPosition()-storedPreference);

    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    });

    preferences xml file:

    <ListPreference
    android:entries="@array/seconds"
    android:entryValues="@array/seconds_values"
    android:summary="sets the seek interval for the seekback and seekforward buttons"
    android:title="Seek Interval"
    android:defaultValue="5000"
    android:key="@string/seek">
    </ListPreference>

    string xml file:

    <string

    name="seek">seek</string>

    array xml file:

    <string-array
    name="seconds">
    <item>Five seconds</item>
    <item>Fifteen seconds</item>
    <item>Thirty seconds</item>
    <item>Sixty seconds</item>
    </string-array>
    <string-array
    name="seconds_values">
    <item>5000</item>
    <item>15000</item>
    <item>30000</item>
    <item>60000</item>

    </string-array>

    Note: I've tried having my array xml file to contain both part string-array and integer-array. having it both string-array and finally both integer-array setup.

    I've also tried various ways (using getInt and getString in the .java file.

    I'm just really burnt out on this one, I hope someone has ran into this issue and now has a solution they can post up for me. I'd really appreciate it.
     

    Advertisement
  2. DroidHead

    DroidHead New Member This Topic's Starter

    Joined:
    Nov 21, 2009
    Messages:
    3
    Likes Received:
    0
    I've solved this issue. It turns out that my project was named with an "_" character and once I refactored the underscore out (and used the getDefaultSharedPreferences option), all is working as should be.
    This issue absorbed 30 hours of my time!
     

Share This Page

Loading...