Create one class
here is complete code how to create SharedPreference
here is complete code how to create SharedPreference
public class SessionManager { SharedPreferences pref; SharedPreferences.Editor editor;// Editor for Shared preferences Context _context; int PRIVATE_MODE = 0; private static final String PREFERENCES = "Preferences"; public static final String UNAME = "uname"; public static final String PNAME = "pname"; public static final String TIME = "time"; public static final String DATE = "date"; public static final String ADDRESS = "address"; private static final String PREF_NAME = "newApp";
public SessionManager(Context context) { this._context = context; pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE); editor = pref.edit(); } public void saveDEtails(String uname, String pname, String time,
String date,String address) {
editor.putString(UNAME, uname); editor.putString(PNAME, pname); editor.putString(TIME, time); editor.putString(DATE, date); editor.putString(ADDRESS, address); editor.commit(); } public HashMapgetUserDetails(){ HashMap user = new HashMap ();
user.put(UNAME, pref.getString(UNAME, null)); user.put(PNAME, pref.getString(PNAME, null)); user.put(TIME, pref.getString(TIME, null)); user.put(DATE, pref.getString(DATE, null)); user.put(ADDRESS, pref.getString(ADDRESS, null));
return user; } }
// And where you want to stored the data in SharedPreference
mSession.saveDEtails(nameu,namep,time,weddingdate,add);SessionManager mSession;mSession = new SessionManager(getApplicationContext());//Here you can get value from SharedPreference
SessionManager mSession;HashMapuser; mSession = new SessionManager(getApplicationContext()); user = mSession.getUserDetails(); String username = user.get(SessionManager.UNAME); // get name String partnername = user.get(SessionManager.PNAME); // get email String time = user.get(SessionManager.TIME); String date = user.get(SessionManager.DATE); String address = user.get(SessionManager.ADDRESS);
No comments:
Post a Comment