Save something in SharedPreferences using SharedPreferences.Editor

If you want to save something (String, setting, value...) in SharedPreferences, such that it can be retrieved later after Activity exited, we can use SharedPreferences.Editor.

Example:

package com.example.androidsharedpreferences;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class MainActivity extends Activity {

String[] dayOfWeek = {"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday"};

EditText editText;
Button buttonSave;
Spinner spinner;

SharedPreferences prefs;
final String KEY_SavedText = "Saved Text";
final String KEY_SavedSel = "Saved Selection";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

editText = (EditText)findViewById(R.id.edittext);
buttonSave = (Button)findViewById(R.id.save);
spinner = (Spinner)findViewById(R.id.spinner);

ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, dayOfWeek);
adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

//retrieve saved preferences
prefs = getPreferences(MODE_PRIVATE);
String prefsString = prefs.getString(KEY_SavedText, null);
if(prefsString != null){
editText.setText(prefsString);
}
int prefsInt = prefs.getInt(KEY_SavedSel, -1);
if(prefsInt != -1){
spinner.setSelection(prefsInt);
}

//Save EditText if buttonSave clicked
buttonSave.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putString(KEY_SavedText, editText.getText().toString());
editor.commit();
}
});

//Save Spinner selection
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){

@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putInt(KEY_SavedSel, position);
editor.commit();
}

@Override
public void onNothingSelected(AdapterView<?> parent) {}
});

}

}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.androidsharedpreferences.MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="lesapplication.blogspot.com" />

<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save Text" />
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

0 Response to "Save something in SharedPreferences using SharedPreferences.Editor"

Posting Komentar