-3

So I just started out with JAVA and I have been trying to save a simple combination of username and password into SharedPreferences. Though i'm not so sure I understand how this actually works. So here is the code I currently have;

public void init() {
    EditText editText = (EditText) findViewById(R.id.password);

    editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;
            if (actionId == EditorInfo.IME_ACTION_SEND) {
                EditText passwordText = (EditText) findViewById(R.id.password);
                String Password =  passwordText.getText().toString();

                EditText usernameText = (EditText) findViewById(R.id.username);
                String Username = usernameText.getText().toString();

                SaveData(Username, Password);
                handled = true;
            }
            return handled;
        }
    });
}


private void SaveData(String Username, String Password) {

    SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString(getString(R.string.saved_high_score), newHighScore);
    editor.commit();
}

Now of course this will not work since I don't have any getActivity(), but I'm wondering what that's supposed to be? Or else how I could save the already available data that i'm sending to the function.

My ideal result would be to be able to get the username and password from another activity. Now I rather not use local files for this but stick with the SharedPreferences.

Much thanks in advance!

killstreet
  • 1,251
  • 2
  • 15
  • 37
  • 1
    I think you should learn first `how to deal with SharedPreferences in Android ?` – M D Sep 23 '16 at 09:24
  • http://stackoverflow.com/questions/30310062/android-cannot-write-object-to-file/30310632#30310632 – kamokaze Sep 23 '16 at 09:29

1 Answers1

2

Hey create a generic class like below and wherever you want to store values in shared preferences you can use it respective function to store paricular datatype.

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

    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){
                SharedPreferences.Editor edit = getSharedPref(mContext).edit();
                edit.putString(key, value);
                edit.commit();
            }
        }

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

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

        public static void setBooleanPrefVal(Context mContext, String key, boolean value) {
            if(key!=null){
                SharedPreferences.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;
        }
    }

If any doubt is there then please ask.

Preetika Kaur
  • 1,991
  • 2
  • 16
  • 23