2016-10-01 15 views
1

一些關鍵的文本,我有一個從我的preferences.xml 保存一些數據,但我希望把網址給我activitity.java把從prefences.xml

這裏我的代碼 ; public static final String PREFS_NAME = "mypreferemce.xml";

,這是我的網址,以獲取來自preference.xml

public void lampu1(View view) { 
    boolean on = ((ToggleButton) view).isChecked(); 
    if (on) { 

     final WebView wv = (WebView) findViewById(R.id.webview5); 
     wv.setWebViewClient(new WebViewClient(){ 
      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
       wv.loadUrl("file:///android_asset/1.html"); 
       Toast.makeText(getApplicationContext(), 
         "koneksi error", Toast.LENGTH_LONG).show(); 
      } 
     }); 
     wv.canGoBack(); 
     wv.loadUrl("http://someurl.com"); 


    } 

一些關鍵的,這是我preference.xml

<string name="mypreference_string">http://someurl.com</string> 

感謝您的幫助

+2

你的問題還不清楚。 mypreference_string是一個字符串變量,不是共享首選項,你想要做什麼。 –

+0

只是將preference從someurl替換爲preference.xml –

+0

您正在使用xml將數據保存到sharedpreference中? –

回答

1

使用下面的代碼化妝這樣的課程以及您需要用於保存任何東西的地方分享偏好使用其方法,並從SharedPreference檢索任何值使用其得到方法。

import android.content.Context; 
import android.content.SharedPreferences; 
import android.content.SharedPreferences.Editor; 

public class SharedPrefManager { 

    public static SharedPreferences getSharedPref(Context mContext) { 
     SharedPreferences pref = mContext.getSharedPreferences(Constants.SETTINGS, Context.MODE_PRIVATE); 

     return pref; 
    } 

    public static void setPrefVal(Context mContext, String key, String value) { 
     if(key!=null){ 
     Editor edit = getSharedPref(mContext).edit(); 
     edit.putString(key, value); 
     edit.commit(); 
     } 
    } 

    public static void setIntPrefVal(Context mContext, String key, int value) { 
     if(key!=null){ 
      Editor edit = getSharedPref(mContext).edit(); 
      edit.putInt(key, value); 
      edit.commit(); 
     } 
    } 

    public static void setLongPrefVal(Context mContext, String key, Long value) { 
     if(key!=null){ 
      Editor edit = getSharedPref(mContext).edit(); 
      edit.putLong(key, value); 
      edit.commit(); 
     } 
    } 

    public static void setBooleanPrefVal(Context mContext, String key, boolean value) { 
     if(key!=null){ 
      Editor edit = getSharedPref(mContext).edit(); 
      edit.putBoolean(key, value); 
      edit.commit(); 
     } 
    } 


    public static String getPrefVal(Context mContext, String key) { 
     SharedPreferences pref = getSharedPref(mContext); 
     String val = ""; 
     try { 
      if (pref.contains(key)) 
       val = pref.getString(key, ""); 
      else 
       val = ""; 
     }catch (Exception e){ 
      e.printStackTrace(); 
     } 
     return val; 
    } 

    public static int getIntPrefVal(Context mContext, String key) { 
     SharedPreferences pref = getSharedPref(mContext); 
     int val = 0; 
     try { 
     if(pref.contains(key)) val = pref.getInt(key, 0); 
     }catch (Exception e){ 
      e.printStackTrace(); 
     } 
     return val; 
    } 

    public static Long getLongPrefVal(Context mContext, String key) { 
     SharedPreferences pref = getSharedPref(mContext); 
     Long val = null; 
     try{ 
     if(pref.contains(key)) val = pref.getLong(key, 0); 
    }catch (Exception e){ 
     e.printStackTrace(); 
    } 
     return val; 
    } 

    public static boolean getBooleanPrefVal(Context mContext, String key) { 
     SharedPreferences pref = getSharedPref(mContext); 
     boolean val = false; 
     try{ 
     if(pref.contains(key)) val = pref.getBoolean(key, false); 

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


    public static boolean containkey(Context mContext,String key) 
    { 
     SharedPreferences pref = getSharedPref(mContext); 
     return pref.contains(key); 
    } 

} 

試試吧,如果需要任何幫助來實現這一點,然後讓我知道。

+0

好吧,這是如此的幫助,現在我需要代碼來代替一些url從共享pref字符串wv.loadUrl(「http://someurl.com」); –

+0

沒有得到你的問題@AhmadYusuf PLZ解釋你想問的問題? –

+0

設置你的網址是這樣的: SharedPrefManager.setPrefVal(this,「url」,「someurl.com」); 你需要寫這樣的東西:wv.loadUrl(SharedPrefManager.getPrefVal(this,「url」)); –