How to display SharedPreference stored data in Fra

2019-01-29 13:59发布

问题:

I stored values in SharedPreference and now I want to access all that in Fragment. When I tried to run my App, it crashed.

public class Credentials extends Fragment {

Button submit, change;
EditText user, id;
Context ctx;

Context context = getActivity();

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

     View windows = inflater.inflate(R.layout.credential, container, false);
        ((TextView)windows.findViewById(R.id.textView)).setText("Credentials");
        submit = ((Button)windows.findViewById(R.id.submit));
        change = ((Button)windows.findViewById(R.id.change));
        user = ((EditText)windows.findViewById(R.id.username));
        id = ((EditText)windows.findViewById(R.id.Useremail));



    SharedPreferences pref = getActivity().getPreferences(Context.MODE_PRIVATE);
    String username = "";
    String email = "";
    pref.getString("username", username);
    pref.getString("email", email);
    Toast.makeText(getActivity(), "Pressed", Toast.LENGTH_SHORT).show();
    user.setText(username);
    id.setText(email);
}

回答1:

I have Created a Util Class for SharedPreference, Hope It will help you.

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

public class SharedPrefrenceUtils {
    public static final String SHARED_PREFERENCE_TAG="TAG_NAME";

    private SharedPrefrenceUtils(){
         throw new AssertionError();
    }

    public static String getString(Context mContext, String key){
        SharedPreferences pref = mContext.getSharedPreferences(SHARED_PREFERENCE_TAG,Activity.MODE_PRIVATE);
        return pref.getString(key, null);
    }

    public static String getString(Context mContext, String key,  String defaultValue){
        SharedPreferences pref = mContext.getSharedPreferences(SHARED_PREFERENCE_TAG,Activity.MODE_PRIVATE);
        return pref.getString(key, defaultValue);
    }

    public static void putString(Context mContext, String key, String value ){
        SharedPreferences pref= mContext.getSharedPreferences(SHARED_PREFERENCE_TAG, Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(key, value);
        editor.commit();
    }


    public static int getInt(Context mContext, String key){
        SharedPreferences pref = mContext.getSharedPreferences(SHARED_PREFERENCE_TAG,Activity.MODE_PRIVATE);
        return pref.getInt(key, 0);
    }

    public static int getInt(Context mContext, String key,  int defaultValue){
        SharedPreferences pref = mContext.getSharedPreferences(SHARED_PREFERENCE_TAG,Activity.MODE_PRIVATE);
        return pref.getInt(key, defaultValue);
    }

    public static void putInt(Context mContext, String key, int value ){
        SharedPreferences pref= mContext.getSharedPreferences(SHARED_PREFERENCE_TAG, Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    public static long getLong(Context mContext, String key){
        SharedPreferences pref = mContext.getSharedPreferences(SHARED_PREFERENCE_TAG,Activity.MODE_PRIVATE);
        return pref.getLong(key, 0);
    }

    public static long getLong(Context mContext, String key,  long defaultValue){
        SharedPreferences pref = mContext.getSharedPreferences(SHARED_PREFERENCE_TAG,Activity.MODE_PRIVATE);
        return pref.getLong(key, defaultValue);
    }

    public static void putLong(Context mContext, String key, long value ){
        SharedPreferences pref= mContext.getSharedPreferences(SHARED_PREFERENCE_TAG, Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putLong(key, value);
        editor.commit();
    }


    public static boolean getBoolean(Context mContext, String key){
        SharedPreferences pref = mContext.getSharedPreferences(SHARED_PREFERENCE_TAG,Activity.MODE_PRIVATE);
        return pref.getBoolean(key, false);
    }

    public static boolean getBoolean(Context mContext, String key,  boolean defaultValue){
        SharedPreferences pref = mContext.getSharedPreferences(SHARED_PREFERENCE_TAG,Activity.MODE_PRIVATE);
        return pref.getBoolean(key, defaultValue);
    }

    public static void putBoolean(Context mContext, String key, boolean value ){
        SharedPreferences pref= mContext.getSharedPreferences(SHARED_PREFERENCE_TAG, Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    public static void remove(Context mContext, String key){
            SharedPreferences pref= mContext.getSharedPreferences(SHARED_PREFERENCE_TAG, Activity.MODE_PRIVATE);
            SharedPreferences.Editor editor = pref.edit();
            editor.remove(key);
            editor.commit();
    }

    public static void clear(Context mContext){
            SharedPreferences pref= mContext.getSharedPreferences(SHARED_PREFERENCE_TAG, Activity.MODE_PRIVATE);
            SharedPreferences.Editor editor = pref.edit();
            editor.clear();
            editor.commit();
    }

}

Now to store String Value: use below statement in your fragment

SharedPrefrenceUtils.putString(mContext,"username", "1234");
SharedPrefrenceUtils.putString(mContext,"email", "xyz@gmail.com");

To get String Value: use below statement in your fragment

String userName = SharedPrefrenceUtils.getString(mContext,"username");
String email = SharedPrefrenceUtils.getString(mContext,"email");

Same way you can store and retrieve any other value from any fragment or activity of your application.



回答2:

There is problem with your receiving method.

public abstract String getString (String key, String defValue)

Retrieve a String value from the preferences.

Parameters

key : The name of the preference to retrieve.
defValue : Value to return if this preference does not exist.

Returns

Returns the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not a String.

So when you use pref.getString() method, you have to store return value to something.

Try below example.

String username = pref.getString("username", "EMPTY");
Toast.makeText(getActivity(), "Value : "+username, Toast.LENGTH_SHORT).show();