I create EditTextPreference in the code like this:
Using java Syntax Highlighting
- import android.app.Activity;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.preference.*;
- public class Settings extends PreferenceActivity {
- private static final String PREFERENCESFILE = "com.qisda.percy.parsexml_preferences.xml";
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setPreferenceScreen(createPreferenceScreen());
- }
- public PreferenceScreen createPreferenceScreen() {
- PreferenceScreen root = getPreferenceManager().createPreferenceScreen(
- this);
- root.setTitle("Settings");
- EditTextPreference city1 = new EditTextPreference(this);
- city1.setKey("city1");
- city1.setTitle("City1");
- city1.setDialogTitle("City1:");
- String aa = city1.getText();
- //city1.getPreferenceManager().getDefaultSharedPreferences(context);
- String str1 = city1.getEditText().getText().toString();
- city1.setSummary(getPreferences("city1"));
- EditTextPreference city2 = new EditTextPreference(this);
- city2.setKey("city2");
- city2.setTitle("City2");
- city2.setDialogTitle("City2:");
- String str2 = city2.getEditText().getText().toString();
- city2.setSummary(getPreferences("city2"));
- String bb = city2.getText();
- root.addPreference(city1);
- root.addPreference(city2);
- return root;
- }
- public String getPreferences(String keyCode)
- {
- SharedPreferences settings = getSharedPreferences(PREFERENCESFILE, 0);
- String preferences = settings.getString(keyCode, "Not set!");
- return preferences;
- }
- }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
in adb shell:
cd /data/data/mypackage/shared_prefs
I can see the XML files used to store the data.
So I want to get the data from the XML files directly,and then use the code to get the data:
Using java Syntax Highlighting
- SharedPreferences settings = getSharedPreferences(PREFERENCESFILE, 0);
- String preferences = settings.getString(keyCode, "Not set!");
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
But I get "null" when the keyCode exactly has values.
I also find in the SDK documents:it say use "String aa = city1.getText();"
can get current preference.But I also get null.
So anyone has any idea about this problem?
Thanks~~

