Shared Preferences in android showing Default value not found

前端 未结 4 1217
滥情空心
滥情空心 2021-01-25 15:14

I strucked in storing and getting shared preferences.When a user has been registered if that registration is success then that values has to be stored in his profile nothing but

4条回答
  •  旧时难觅i
    2021-01-25 15:56

    To save value:

      @Override
              protected void onPostExecute(String s) {
    
                  pd.dismiss();
    
                        if("SUCCESS".equals(jsonresponce)){
                            Toast.makeText(getApplicationContext(),jsonresponce,Toast.LENGTH_SHORT).show();
                             SharedPreferences sharedpreferences = getSharedPreferences("a2a", Context.MODE_PRIVATE);
                            SharedPreferences.Editor editor = sharedpreferences.edit();
    
                            editor.putString("enteredfname", enteredfname);
                            editor.putString("enteredlname", enteredlname);
                            editor.commit();
    
                        }else{
                            Toast.makeText(getApplicationContext(),jsonresponce,Toast.LENGTH_SHORT).show();
                        }
    
                  super.onPostExecute(s);
              }
          }
    

    To retrive value:

     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_myprofile, container, false);
    
        mfname = (EditText)rootView.findViewById(R.id.fname);
        mlname = (EditText)rootView.findViewById(R.id.lname);
        mcontact = (EditText)rootView.findViewById(R.id.contact);
        mpassword = (EditText)rootView.findViewById(R.id.editpass);
        SharedPreferences sharedpreferences = getSharedPreferences("a2a", Context.MODE_PRIVATE)
    
        value = sharedpreferences.getString("enteredfname", "enteredfname")
    
        mfname.setText(value);
        return rootView;
    }
    

提交回复
热议问题