Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Student.java
- ------------
- package com.example.mohamadpc.savedataexample;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.util.Log;
- /**
- * Created by MOHAMADPC on 17/10/2017.
- */
- public class Student {
- private String name;
- private String clas;
- private String password;
- private int idNo;
- public Student(String name, String clas, String password, int idNo) {
- this.name = name;
- this.clas = clas;
- this.password = password;
- this.idNo = idNo;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getClas() {
- return clas;
- }
- public void setClas(String clas) {
- this.clas = clas;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public int getIdNo() {
- return idNo;
- }
- public void setIdNo(int idNo) {
- this.idNo = idNo;
- }
- @Override
- public String toString() {
- return "Student{" +
- "name='" + name + '\'' +
- ", clas='" + clas + '\'' +
- ", password='" + password + '\'' +
- ", idNo=" + idNo +
- '}';
- }
- }
- Utils.java
- ----------
- package com.example.mohamadpc.savedataexample;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.util.Log;
- import static android.R.attr.name;
- import static android.R.attr.password;
- /**
- * Created by MOHAMADPC on 17/10/2017.
- */
- public class Utils {
- public static int[] getAllIdNo(Context context) {
- SharedPreferences sp = context.getSharedPreferences("StudentsSp", Context.MODE_PRIVATE);
- Object[] keys = sp.getAll().keySet().toArray();
- int[] res = new int[keys.length];
- for (int i = 0; i < res.length; i += 1) {
- res[i] = Integer.parseInt(keys[i].toString());
- }
- return res;
- }
- public static boolean checkLogin(Context context, int idNo, String password) {
- SharedPreferences sp = context.getSharedPreferences("StudentsSp", Context.MODE_PRIVATE);
- return sp.getString(idNo + "", "").split("\\|")[2].equals(password);
- }
- public static boolean save(Context context, Student st) {
- SharedPreferences sp = context.getSharedPreferences("StudentsSp", Context.MODE_PRIVATE);
- String dataStr;
- dataStr = st.getName() + "|" + st.getClas() + "|" + st.getPassword();
- return sp.edit().putString(st.getIdNo() + "", dataStr).commit();
- }
- public Student load(Context context, int idNo) {
- SharedPreferences sp = context.getSharedPreferences("StudentsSp", Context.MODE_PRIVATE);
- String dataStr = sp.getString(idNo + "", "");
- String[] parts = dataStr.split("\\|");
- if (parts.length == 3) {
- return new Student(parts[0], parts[1], parts[2], idNo);
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement