-2

I have this button code and it was connect to database

bLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String username = etUsername.getText().toString();
            final String password = etPassword.getText().toString();

            // Response received from the server
            Response.Listener<String> responseListener = new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject jsonResponse = new JSONObject(response);
                        boolean success = jsonResponse.getBoolean("success");

                        if (success) {
                            String name = jsonResponse.getString("name");
                            String email = jsonResponse.getString("email");
                            String alamat = jsonResponse.getString("alamat");
                            int notelp = jsonResponse.getInt("notelp");
                            String username = jsonResponse.getString("username");
                            String password = jsonResponse.getString("password");

                            Intent intent = new Intent(MainActivity.this, adminarea.class);
                            intent.putExtra("name", name);
                            intent.putExtra("email", email);
                            intent.putExtra("alamat", alamat);
                            intent.putExtra("notelp", notelp);
                            intent.putExtra("username", username);
                            intent.putExtra("password", password);
                            MainActivity.this.startActivity(intent);
                        } else {
                            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                            builder.setMessage("Login Gagal")
                                    .setNegativeButton("Ulangi", null)
                                    .create()
                                    .show();
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            };

i have a table called admin thats contain field name, email and etc.. how can i take the data from databse then save my data like name, email, alamat and the other to shared prefences. then can you help me to call it to another activity?

Priyanka
  • 67
  • 1
  • 6
Sii Ann Die
  • 27
  • 1
  • 6

8 Answers8

5

First of all you have to create SharedPreferences

SharedPreferences pref = getApplicationContext().getSharedPreferences("YOUR_PREF_NAME", MODE_PRIVATE); 
Editor edt = pref.edit();

To add value in preference:

edt.putString("name", name);           
edt.putString("email", email);       
edt.putString("alamat", alamat);   
edt.putString("notelp", notelp);     
edt.putString("username",username); 

// Save your changes
edt.commit();

Now for getting data from preference:

String name=pref.getString("name", null);  
String email=pref.getString("email", null);  
String alamat=pref.getString("alamat", null);  
String notelp=pref.getString("notelp", null);  
String username=pref.getString("username", null);

If you want to clear the preference data:

edt.clear();
edt.commit(); 
Dhruv
  • 1,794
  • 3
  • 19
  • 37
1

Save like below.

        SharedPreferences getPref = PreferenceManager.getDefaultSharedPreferences(this);

        Intent intent = new Intent(MainActivity.this, adminarea.class);

        intent.putExtra("name", name);
        getPref.edit().putString("name",name).apply();
        intent.putExtra("email", email);
        getPref.edit().putString("email",name).apply();
        intent.putExtra("alamat", alamat);
        getPref.edit().putString("alamat",name).apply();
        intent.putExtra("notelp", notelp);
        intent.putExtra("username", username);
        intent.putExtra("password", password);

        MainActivity.this.startActivity(intent);

Retrieve like this.

        SharedPreferences getPref = PreferenceManager.getDefaultSharedPreferences(this);
        String name = getPref.getString("name","");
        String email = getPref.getString("email","");
        String alamat = getPref.getString("alamat","");
Sohail Zahid
  • 7,738
  • 2
  • 22
  • 34
1

Create a class SharePrefUtil

private Context context;
private SharedPreferences prefs;

public SharePrefUtil(Context mContext) {
    this.context = mContext;
    prefs = this.context.getSharedPreferences("your package name", Context.MODE_PRIVATE);
}


public void setValueInSharePref(String keyName, String value) {
    prefs.edit().putString(keyName, value).apply();
}

public String getValueFromSharePref(String keyName) {
    return prefs.getString(keyName, "");
}
}

When ever you want to store the value Just Make a object of class in you activity to get or set a value for example

SharePreUtil shef = new SharePreUtil(this);

When ever you what to set a value in the activity

shef.setValueInSharePref("key", "yourvalue");

when you want to get value

shef.getValueFromSharePref("key");

All the best...

rak
  • 68
  • 12
0

You can save data in SharedPreferences in your activity class as below:

  SharedPreferences sharedPreferences = getSharedPreferences("USER_INFO", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString("name", name);
    editor.putString("email", alamat);
    editor.putString("alamat", alamat);
    editor.commit();

And in your activity where you want to fetch them, do as below:

  SharedPreferences sharedPreferences = getSharedPreferences("USER_INFO", Context.MODE_PRIVATE);
    String name=sharedPreferences.getString("name", "abc");
    String email=sharedPreferences.getString("email", "abc@gmail.com");

To put int,

editor.putInt("number",0);    

And to retrive it,

sharedPreferences.getInt("number",0);
Dhruvi
  • 1,896
  • 1
  • 8
  • 18
0
onCreate() method
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

to get all data server after
 SharedPreferences.Editor editor = sharedpreferences.edit();
                    editor.putString(name, name);
                    editor.putString(email, email);
                    .....
                    editor.commit();
Ramesh Prajapati
  • 584
  • 5
  • 10
0

quoting the training guide at https://developer.android.com/training/basics/data-storage/shared-preferences.html

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

and to read

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger(R.string.saved_high_score_default);
long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);
g90
  • 496
  • 3
  • 15
0

In your other activity....first receive data from intent...like this

String name = getIntent().getExtras().getInt("name");

now,save this data into shared prefrences....to do that...you need sharedPreferences.Editor to write data.....like this...

 SharedPreferences sp = Your_Activity.this.getSharedPreferences("SharedPreferences_Name", Context.MODE_PRIVATE);
                    SharedPreferences.Editor spwriter = sp.edit();
                    spwriter.putString("name",name);
                    spwriter.commit();
Intimate
  • 397
  • 3
  • 18
0
if (success) {

    // once you retrieve all the required data
    SharedPreferences prefs = this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);

    // Writing data to SharedPreferences
    Editor editor = prefs.edit();
    editor.putString("name", name);
    editor.putString("email", email);
    editor.putInt("notelp", notelp);
    editor.commit();

    // start your AdminArea activity here
}

Retrieve data inn AdminArea class:

// Reading from SharedPreferences
String value = prefs.getString("name", "DEFAULT_STRIGN"); // if the given key not found, it'll take the default string as value
int intValue = prefs.getInt("notelp", -1); // instead of -1, you can give any default value 

hope this helps :)

Mr.7
  • 1,937
  • 1
  • 17
  • 29