[Issue]How to get data from EditTextPreference

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

[Issue]How to get data from EditTextPreference

Postby pjq » Fri May 22, 2009 4:32 am

Hi All,
I create EditTextPreference in the code like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. import android.app.Activity;
  3.  
  4. import android.content.SharedPreferences;
  5.  
  6. import android.os.Bundle;
  7.  
  8. import android.preference.*;
  9.  
  10.  
  11.  
  12. public class Settings extends PreferenceActivity {
  13.  
  14.        
  15.  
  16.         private static final String PREFERENCESFILE = "com.qisda.percy.parsexml_preferences.xml";
  17.  
  18.  
  19.  
  20.         public void onCreate(Bundle savedInstanceState) {
  21.  
  22.                 super.onCreate(savedInstanceState);
  23.  
  24.  
  25.  
  26.                 setPreferenceScreen(createPreferenceScreen());
  27.  
  28.  
  29.  
  30.         }
  31.  
  32.  
  33.  
  34.         public PreferenceScreen createPreferenceScreen() {
  35.  
  36.                 PreferenceScreen root = getPreferenceManager().createPreferenceScreen(
  37.  
  38.                                 this);
  39.  
  40.  
  41.  
  42.                 root.setTitle("Settings");
  43.  
  44.  
  45.  
  46.                 EditTextPreference city1 = new EditTextPreference(this);
  47.  
  48.                 city1.setKey("city1");
  49.  
  50.                 city1.setTitle("City1");
  51.  
  52.                 city1.setDialogTitle("City1:");
  53.  
  54.                 String aa = city1.getText();
  55.  
  56.                 //city1.getPreferenceManager().getDefaultSharedPreferences(context);
  57.  
  58.  
  59.  
  60.                 String str1 = city1.getEditText().getText().toString();
  61.  
  62.                 city1.setSummary(getPreferences("city1"));
  63.  
  64.  
  65.  
  66.                 EditTextPreference city2 = new EditTextPreference(this);
  67.  
  68.                 city2.setKey("city2");
  69.  
  70.                 city2.setTitle("City2");
  71.  
  72.                 city2.setDialogTitle("City2:");
  73.  
  74.                 String str2 = city2.getEditText().getText().toString();
  75.  
  76.                 city2.setSummary(getPreferences("city2"));
  77.  
  78.                 String bb = city2.getText();
  79.  
  80.  
  81.  
  82.                 root.addPreference(city1);
  83.  
  84.                 root.addPreference(city2);
  85.  
  86.  
  87.  
  88.                 return root;
  89.  
  90.  
  91.  
  92.         }
  93.  
  94.        
  95.  
  96.         public String getPreferences(String keyCode)
  97.  
  98.         {
  99.  
  100.                 SharedPreferences settings = getSharedPreferences(PREFERENCESFILE, 0);
  101.  
  102.                 String preferences = settings.getString(keyCode, "Not set!");
  103.  
  104.                 return preferences;
  105.  
  106.         }
  107.  
  108.  
  109.  
  110. }
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
Parsed in 0.036 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:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. SharedPreferences settings = getSharedPreferences(PREFERENCESFILE, 0);
  2.  
  3.                 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~~
pjq
Freshman
Freshman
 
Posts: 7
Joined: Fri May 15, 2009 1:36 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 8 guests