2012-08-29 85 views
-1

我在android系統保存數據SharedPreferencies的Android

 public SiriusRadioPlayerActivity(){ 
    String[] arrayOfString = new String[4]; 
    arrayOfString[0] = "Kiskunfélegyháza HQ [160kbps MP3]"; 
    arrayOfString[1] = "Kiskunfélegyháza MQ [64kbps AAC]"; 
    arrayOfString[2] = "Kiskunmajsa HQ [160kbps MP3]"; 
    arrayOfString[3] = "Kiskunmajsa MQ [64kbps AAC]"; 
    this.items = arrayOfString; 

    }   

做了微調這些都是我的琴絃顯示!

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    initializeUIElements(); 

this.savszelesseg_port = localSharedPreferences.getString("savszelesseg_mentes",  "savszelesseg_port"); 
    this.savszelesseg_saved = this.savszelesseg_port; 

這裏我從我的首選項加載數據!

final Spinner localSpinner = (Spinner)findViewById(R.id.savszelesseg); 
    ArrayAdapter localArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item, this.items); 
    localArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    localSpinner.setAdapter(localArrayAdapter); 

創建微調。並具有en setOnItemSelectedListener函數。

localSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 
    { 
     public void onItemSelected(AdapterView parentView, View childView, int position, long id) 
     { 
      if (!savszelesseg_port_modositva) 
      { 
       savszelesseg_beallitas = "Kiskunfélegyháza HQ [160kbps MP3]"; 
       savszelesseg_port_modositva = true; 
      } 
if (savszelesseg_beallitas == "Kiskunfélegyháza HQ [160kbps MP3]") 
      { 
       savszelesseg_port = "8500/relay_911"; 

      } 


     } 
     public void onNothingSelected(AdapterView parentView) 
     { 
     } 
    }); 

的問題是:如何savszelesseg_port存儲到我的SharedPreferencies savszelesseg_mentes默認值?因爲我有一個計時器定期檢查網頁,它取決於我從sharedpreferencies獲得其值:

this.mWebView = ((WebView)findViewById(0x7f070035)); 
    this.mWebView.getSettings().setJavaScriptEnabled(true); 
    new Timer().scheduleAtFixedRate(new TimerTask() 
    { 
     public void run() 
     { 
     if (SiriusRadioPlayerActivity.isOnline()){ 

      if (SiriusRadioPlayerActivity.this.mWebView_enabled == false){ 
       SiriusRadioPlayerActivity.this.mWebView.loadUrl("about:blank"); 
      } 
      else 
      { 

       if(savszelesseg_port.contains("911")){ 
       SiriusRadioPlayerActivity.this.mWebView.loadUrl("http://xxxx");  
       } 
       if(savszelesseg_port.contains("882")){ 
       SiriusRadioPlayerActivity.this.mWebView.loadUrl("http://xxx"); 
       } 

       } 

     } 



     }} 
    , this.delay, this.period); 
    this.mWebView.setBackgroundColor(0); 

Localsharedpreferencies:

PreferenceManager.setDefaultValues(this, 0x7f040000, false); 
    final SharedPreferences localSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); 
    this.mWebView_enabled = localSharedPreferences.getBoolean("mWebView_enabled", true); 
    this.savszelesseg_port = localSharedPreferences.getString("savszelesseg_mentes", "savszelesseg_port"); 
+0

什麼是localSharedPreferences? – Sameer

回答

0

行後:

  savszelesseg_port = "8500/relay_911"; 

你應該這樣做:

SharedPreferences.Editor editor = localSharedPreferences.edit(); 
editor.putString("savszelesseg_mentes", savszelesseg_port); 
editor.commit();