tags:

views:

302

answers:

3

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?

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">

From strings file:

seek

From an array file:

Five seconds Fifteen seconds Thirty seconds Sixty seconds 5000 15000 30000 60000

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.

Arg, not sure how to get the code and plain text to format nicely here, even tried the options, like Code Sample, no luck

AndroidCoder

A: 

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);
Pentium10
Thanks for your input, please bare with me. I've changed out to SharedPreferences preferences = PreferenceManager .getDefaultSharedPreference(getBaseContext()); However, I'm debugging directly on a phone. I see the path to my project, but it's at F:\data\com.mypackage and all I see is the one file that should be writing to that directory (which works fine, both reading and writing to it). Ps. I mounted my phone's SD, whould that give me an accurate path view or do I need to go the adb route as you noted here? Thanks again for the help.
You are doing fine, check the pref file and open in an editor, and check out if the key exists and holds the correct value.
Pentium10
It looks like the value serialized out properly. I opened it in EditPlus 3 and see android:key="@string/seek". It's also in the res/xml directory.
One thing I noticed is that when I first started this project I created preferences.xml and Preferences.java (note the upper-case "P" character). Would this be an issue? I do not see any design-time errors thrown, nor at runtime.
Oh, I was just reading back on your question. The file being written is another file (.3gpp). I do not see any other files in my project directory on the phone. Sorry for the confusion there.
Well certainly you are doing something wrong, or your settings are not saved, so the file doesn't exists yet. For my application the .XML file is at `/data/data/com.myapp/shared_prefs/com.myapp_preferences.xml` You need to figure out to get some files in `shared_prefs` folder.
Pentium10
Thanks. I'll try a sample project from scratch. This is odd for sure.
Just run the examples from the ApiDemos (check your android install folder for samples /platforms/.../samples
Pentium10
okay. I'll still beating my head, let me give the ApiDemos a twirl. Thanks again for your patience, help.
You have a low rate. Important on SO, you have to mark accepted answers by using the tick on the left of the posted answer, below the voting. This will increase your rate.
Pentium10
A: 

.............................

IdeaGent
I found the ApiDemos and looked through such, not really close enough to what I need. But, a good collectioin of samples, thanks for that.Would you be willing to post your sample preferences project so I can compare each file (ListPreference with Integer values is what I'm needing, doing now).I can understand this is a bit to ask. But, if you can find your way to upload to say box.net or http://www.yousendit.com/ which makes it very easy. That would be super!
IdeaGent
I think my next step is to go to a for-hire Android development team or consultant to get this sorted. It's beyond me. I've put way to many hours into this, I'm not sure why this is so hard to have done.
IdeaGent
+1  A: 

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!

AndroidCoder