开发者

getSharedPreferences not working for me with concerns to ListPreferences and Integers

I'm stuck at a point where I'm trying to get my project to read a preference value (from a ListPreference listing) and then use that value in a basic mathematical subtraction instance. The problem is that the "seek" preference is not being seen by my Java code, and yet the default value is (I've tried the default value with 3000 and now 0). Am i missing something, is there a bug here, known or unknown?

Here is my Java code chunk where the issues manifests itself:

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);

(mediaPlayer.getCurrentPosition()-storedPreference);

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

Here are some other code bits for my project:

From preferences 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>

From strings file:

<string name="seek">seek</string>

From开发者_Go百科 an array file:

<resources>
    <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>
</resources>

let me know if you need to see more code to figure this one out

Thanks in advance for any help that can be offered. I've worked over this issue now for a few hours and I'm burnt, a second pair of eyes on it would be very much appreciated.


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!


Make sure you store the ListPreference values in the same files. Start up adb roll to the cd /data/data/com.your.package and look for folders and files of type preferences.

I think the bug is that you specify a different file than the one the setting has been saved too:

Try changing this:

SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);

to

SharedPreferences preferences = PreferenceManager
                .getDefaultSharedPreferences(context);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜