Advertisement
mmayoub

School, save data to shared preferences fils

Oct 17th, 2017
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 KB | None | 0 0
  1. Student.java
  2. ------------
  3. package com.example.mohamadpc.savedataexample;
  4.  
  5. import android.content.Context;
  6. import android.content.SharedPreferences;
  7. import android.util.Log;
  8.  
  9. /**
  10.  * Created by MOHAMADPC on 17/10/2017.
  11.  */
  12.  
  13. public class Student {
  14.     private String name;
  15.     private String clas;
  16.     private String password;
  17.     private int idNo;
  18.  
  19.     public Student(String name, String clas, String password, int idNo) {
  20.         this.name = name;
  21.         this.clas = clas;
  22.         this.password = password;
  23.         this.idNo = idNo;
  24.     }
  25.  
  26.  
  27.     public String getName() {
  28.         return name;
  29.     }
  30.  
  31.     public void setName(String name) {
  32.         this.name = name;
  33.     }
  34.  
  35.     public String getClas() {
  36.         return clas;
  37.     }
  38.  
  39.     public void setClas(String clas) {
  40.         this.clas = clas;
  41.     }
  42.  
  43.     public String getPassword() {
  44.         return password;
  45.     }
  46.  
  47.     public void setPassword(String password) {
  48.         this.password = password;
  49.     }
  50.  
  51.     public int getIdNo() {
  52.         return idNo;
  53.     }
  54.  
  55.     public void setIdNo(int idNo) {
  56.         this.idNo = idNo;
  57.     }
  58.  
  59.     @Override
  60.     public String toString() {
  61.         return "Student{" +
  62.                 "name='" + name + '\'' +
  63.                 ", clas='" + clas + '\'' +
  64.                 ", password='" + password + '\'' +
  65.                 ", idNo=" + idNo +
  66.                 '}';
  67.     }
  68.  
  69.  
  70. }
  71.  
  72.  
  73. Utils.java
  74. ----------
  75. package com.example.mohamadpc.savedataexample;
  76.  
  77. import android.content.Context;
  78. import android.content.SharedPreferences;
  79. import android.util.Log;
  80.  
  81. import static android.R.attr.name;
  82. import static android.R.attr.password;
  83.  
  84. /**
  85.  * Created by MOHAMADPC on 17/10/2017.
  86.  */
  87.  
  88. public class Utils {
  89.  
  90.     public static int[] getAllIdNo(Context context) {
  91.         SharedPreferences sp = context.getSharedPreferences("StudentsSp", Context.MODE_PRIVATE);
  92.         Object[] keys = sp.getAll().keySet().toArray();
  93.  
  94.         int[] res = new int[keys.length];
  95.         for (int i = 0; i < res.length; i += 1) {
  96.             res[i] = Integer.parseInt(keys[i].toString());
  97.         }
  98.  
  99.         return res;
  100.     }
  101.  
  102.     public static boolean checkLogin(Context context, int idNo, String password) {
  103.         SharedPreferences sp = context.getSharedPreferences("StudentsSp", Context.MODE_PRIVATE);
  104.         return sp.getString(idNo + "", "").split("\\|")[2].equals(password);
  105.     }
  106.  
  107.     public static boolean save(Context context, Student st) {
  108.         SharedPreferences sp = context.getSharedPreferences("StudentsSp", Context.MODE_PRIVATE);
  109.         String dataStr;
  110.         dataStr = st.getName() + "|" + st.getClas() + "|" + st.getPassword();
  111.  
  112.         return sp.edit().putString(st.getIdNo() + "", dataStr).commit();
  113.     }
  114.  
  115.     public Student load(Context context, int idNo) {
  116.         SharedPreferences sp = context.getSharedPreferences("StudentsSp", Context.MODE_PRIVATE);
  117.         String dataStr = sp.getString(idNo + "", "");
  118.  
  119.         String[] parts = dataStr.split("\\|");
  120.  
  121.         if (parts.length == 3) {
  122.             return new Student(parts[0], parts[1], parts[2], idNo);
  123.         }
  124.         return null;
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement