Bu dersimizde activity düzeyinde yani sadece o activity’e özgü olan preference dosyasına nasıl veri yazıp, bu veriyi nasıl okuyacağımızdan bahsediyoruz. Ayrıca bu oluşturulan dosya nerededir ve nasıl erişebiliriz sorularına cevap veriyoruz.
Activity düzeyinde dosya oluşturmak için getPreference methodunu kullanıyoruz. Bu method bize bir SharedPreference nesnesi geriye döndürür. Dosyamıza veri yazacaksak SharedPreference.Editor sabit sınıfından bir nesne üretip, bu nesneye getPreference ile geri dönen SharedPreference nesnesini edit modda atıyoruz.
1 2 |
SharedPreferences sharedPreferences=getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor=sharedPreferences.edit(); |
Editör nesnesini kullanarak key-value ilişkisi içerisinde dosyamıza verileri yazıyoruz. İşlemler bittikten sonra editor.apply() veya editor.commit() methodlarıyla yaptığımız işlemlerin onaylanmasını ve verilerin dosyaya yazılmasını sağlamamız gerekiyor, aksi taktirde dosyaya herhangi bir veri yazılmaz. Bu methotlar aynı işi yapsa da aralarında ufak farklılıklar vardır, açıklamayı videoda bulabilirsiniz.
Dosyadan verileri okurken editor nesnesi oluşturmaya gerek yoktur. sharedPreference nesnesini oluşturup get methodlarıyla kaydettiğimiz verilerin anahtar değerlerini yazarak dosyadan verileri okuyabiliriz.
Bu dersimizde yazılan kodları aşağıda bulabilirsiniz. SharedPreferenceDemo projesinin bitmiş son halini buradaki linkten indirebilirsiniz.
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 |
public class MainActivity extends AppCompatActivity { private EditText etIsim, etMeslek; private TextView tvIsim, tvMeslek, tvID; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } public void init(){ etIsim= (EditText) findViewById(R.id.etName); etMeslek= (EditText) findViewById(R.id.etProfession); tvIsim= (TextView) findViewById(R.id.txvName); tvMeslek= (TextView) findViewById(R.id.txvProfession); tvID= (TextView) findViewById(R.id.txvID); } public void saveAccountData(View view) { SharedPreferences sharedPreferences=getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor=sharedPreferences.edit(); editor.putString(Sabitler.KEY_ISIM, etIsim.getText().toString()); editor.putString(Sabitler.KEY_MESLEK, etMeslek.getText().toString()); editor.putInt(Sabitler.KEY_ID, 150); editor.apply(); Toast.makeText(this, "Veriler Kaydedildi", Toast.LENGTH_SHORT).show(); //editor.commit(); } public void loadAccountData(View view) { SharedPreferences sharedPreferences=getPreferences(Context.MODE_PRIVATE); String isim=sharedPreferences.getString(Sabitler.KEY_ISIM,"N/A"); String meslek=sharedPreferences.getString(Sabitler.KEY_MESLEK, "N/A"); int id=sharedPreferences.getInt(Sabitler.KEY_ID, 0); tvIsim.setText(isim); tvMeslek.setText(meslek); tvID.setText(""+id); } } |
1 2 3 4 5 6 7 |
public class Sabitler { public static final String KEY_ISIM= "isim"; public static final String KEY_MESLEK= "meslek"; public static final String KEY_ID= "id"; } |
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 65 66 67 68 69 70 71 72 73 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/pageLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="20dp" tools:context="com.example.emrealtunbilek.sharedpreferencesornek.MainActivity"> <Switch android:id="@+id/pageColorSwitch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Yeşile Çevir "/> <EditText android:id="@+id/etName" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="74dp" android:hint="Adınızı giriniz"/> <EditText android:id="@+id/etProfession" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Mesleğinizi giriniz"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" android:onClick="saveAccountData" android:text="KAYDET"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="loadAccountData" android:text="GETİR"/> </LinearLayout> <TextView android:id="@+id/txvName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="30dp" android:text="Kişi Adı:" android:textColor="#969494" android:textSize="25sp" android:textStyle="bold"/> <TextView android:id="@+id/txvProfession" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Meslek:" android:textColor="#969494" android:textSize="20sp" android:textStyle="bold"/> </LinearLayout> |