put some key text from prefences.xml

笑着哭i 提交于 2020-01-05 05:33:10

问题


i have a save some data from my preferences.xml but i want to put url to my activitity.java

here my code ;public static final String PREFS_NAME = "mypreferemce.xml";

and this is my url to get some key from 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");


    }

and this is my preference.xml

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

thanks for help


回答1:


Use below code make a class like this and wherever you need to use to save any thing in shared preference use its set method and to retrieve any value from SharedPreference use its get method.

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);
    }   

}

Try it if any help is needed to implement this then let me know.



来源:https://stackoverflow.com/questions/39803521/put-some-key-text-from-prefences-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!