Pages

Thursday, December 1, 2016

Tutorial Membuat SharedPreferences pada Android menggunakan Eclipse

SharedPreference adalah penyimpanan data yang paling sederhana yang tersimpan pada internal memory. Data yang disimpan dengan metode ini hanyalah data primitif seperti boolean, int, string, float, dan long. Data disimpan dengan konsep pasangan Key dengan Value. Key digunakan sebagai id dari data dan Value adalah nilai datanya.

 

langsung saja kita buat project baru. buatlah layout pada activity_main.xml seperti dibawah ini.
activity_main.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<RelativeLayout 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"
    tools:context=".MainActivity" >
 
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
 
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="20dp"
        android:text="NPM" />
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText2"
        android:layout_below="@+id/editText2"
        android:layout_marginTop="20dp"
        android:text="Simpan" />
 
    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText2"
        android:layout_below="@+id/textView1"
        android:ems="10" />
 
    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="15dp"
        android:text="NAMA" />
 
    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_below="@+id/textView3"
        android:ems="10" />
 
</RelativeLayout>

Pada file ActivityMain.java pastekan koding berikut.
ActivityMain.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.example.sharep;
 
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
 
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;
 
public class MainActivity extends Activity {
 EditText nim, nama;
 Button simPan;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  simPan = (Button) findViewById(R.id.button1);
  nim = (EditText) findViewById(R.id.editText1);
  nama = (EditText) findViewById(R.id.editText2);
  tampilkan();
  simPan.setOnClickListener(new OnClickListener() {
 
   @Override
   public void onClick(View arg0) {
    // TODO Auto-generated method stub
 
    saVePre("NIM", nim.getText().toString());
    saVePre("NAMA", nama.getText().toString());
 
    finish();
   }
  });
 }
 
 private void tampilkan() {
  SharedPreferences sp = PreferenceManager
    .getDefaultSharedPreferences(this);
 
  String nimm = sp.getString("NIM", "");
  String namaa = sp.getString("NAMA", "");
 
  nim.setText(nimm);
  nama.setText(namaa);
 
 }
 
 private void saVePre(String key, String value) {
  SharedPreferences sp = PreferenceManager
    .getDefaultSharedPreferences(this);
  Editor edit = sp.edit();
  edit.putString(key, value);
  edit.commit();
 }
}


Jalankan project anda, masukkan data di form npm dan nama lalu klik simpan. lalu buka kembali aplikasi anda, maka data yang tadinya anda masukkan kini tampil di form.




Demikian lah sedikit banyaknya yang dapat saya sampikan lebih dan kurangnya saya mohon maaf...saya akhiri assalamualaikum warahmatullahi wabarakatuh.

No comments:

Post a Comment