Bukan Materi Pelajaran yang Sulit, tapi Memulai Untuk Belajar itu yang Sulit.

PreferencedConnector Code Using For Save String Value in Cache Memory Application Android

4:05 PM Posted by Unknown No comments

بِسْمِ اللهِ الرَّحْمٰنِ الرَّحِيْمِ


Create File "PreferencedConnector.java" and copy code like this:

package com.rki.demokrado.storege;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class PreferencedConnector{
 
 public static final String PREF_NAME = "DATA_PREFERENCES";
 public static final int MODE = Context.MODE_PRIVATE;
 //DEMOKRADO 
 public static String ID_USERS = "ID_USERS";
 public static String FULLNAME = "FULLNAME";
 public static String EMAIL = "EMAIL";
 public static String PASS = "PASS";
 public static String USERNAME = "USERNAME";
 public static String FEED_TITLE= "FEED_TITLE";
 public static String LOGOUT= "LOGOUT";
 
 public static void writeBoolean(Context context, String key, boolean value) {
  getEditor(context).putBoolean(key, value).commit();
 }

 public static boolean readBoolean(Context context, String key, boolean defValue) {
  return getPreferences(context).getBoolean(key, defValue);
 }

 public static void writeInteger(Context context, String key, int value) {
  getEditor(context).putInt(key, value).commit();

 }

 public static int readInteger(Context context, String key, int defValue) {
  return getPreferences(context).getInt(key, defValue);
 }

 public static void writeString(Context context, String key, String value) {
  getEditor(context).putString(key, value).commit();

 }
 
 public static String readString(Context context, String key, String defValue) {
  return getPreferences(context).getString(key, defValue);
 }
 
 public static void writeFloat(Context context, String key, float value) {
  getEditor(context).putFloat(key, value).commit();
 }

 public static float readFloat(Context context, String key, float defValue) {
  return getPreferences(context).getFloat(key, defValue);
 }
 
 public static void writeLong(Context context, String key, long value) {
  getEditor(context).putLong(key, value).commit();
 }

 public static long readLong(Context context, String key, long defValue) {
  return getPreferences(context).getLong(key, defValue);
 }

 public static SharedPreferences getPreferences(Context context) {
  return context.getSharedPreferences(PREF_NAME, MODE);
 }

 public static Editor getEditor(Context context) {
  return getPreferences(context).edit();
 }
}


And then you save Value string to cache use this code:

PreferencedConnector.writeString(activity.getApplicationContext(), PreferencedConnector.FEED_TITLE, title.getText().toString());

And the last step, you can calling Value string saved to cache like this code:

tv_title.setText(PreferencedConnector.readString(getApplicationContext(), PreferencedConnector.FEED_TITLE, null));

finish,, good try and good luck.

0 komentar:

Post a Comment