Friday, 4 August 2017

Store Complete Object in SharedPrefrence


Add Given Dependancy in your gradle file-
compile 'com.google.code.gson:gson:2.7'
and Add this class -

public class Shprefrences {

    public static final String PREFERENCES = "Data";
    Context context;
    SharedPreferences sharedpreferences;
    SharedPreferences.Editor editor;

    public void clearData()
    {
        editor.remove("Data");
        editor.clear();
        editor.commit();
    }

    public Shprefrences(Context context) {
        this.context = context;
        sharedpreferences = context.getSharedPreferences(PREFERENCES,
 Context.MODE_PRIVATE);
        editor = sharedpreferences.edit();
    }

    public void setString(String key, String val) {
        editor.putString(key, val);
        editor.commit();
    }

    public void setBoolean(String key, boolean val) {
        editor.putBoolean(key, val);
        editor.commit();
    }

    public void setInt(String key, int val) {
        editor.putInt(key, val);
        editor.commit();
    }


    public String getString(String key, String val) {
        return sharedpreferences.getString(key,val).toString();
    }

    public boolean getBoolean(String key, boolean val) {
        return sharedpreferences.getBoolean(key,val);
    }


    public void setFrmBasicObject(String key, FormDataModel obj)
    {
        Gson gson = new Gson();
        String json = gson.toJson(obj);
        editor.putString(key, json);
        editor.commit();
    }

    public FormDataModel getFrmBasicObject(String key)
    {
        Gson gson = new Gson();
        String json = sharedpreferences.getString(key, "");
        FormDataModel ob= gson.fromJson(json, FormDataModel.class);
        return ob;
    }

    public void setCompanyModel(String key, ArrayList<CompanyNameModel> obj)
    {
        Gson gson = new Gson();
        String json = gson.toJson(obj);
        editor.putString(key, json);
        editor.commit();
    }

    public ArrayList<CompanyNameModel> getComapnyModel(String key)
    {
        Gson gson = new Gson();
        String json = sharedpreferences.getString(key, "");
        Type type = new TypeToken<ArrayList<CompanyNameModel>>() {}.getType();
        ArrayList<CompanyNameModel> arrayList = gson.fromJson(json, type);
        return arrayList;
    }


    public void setLoginModel(String key, LoginModel obj)
    {
        Gson gson = new Gson();
        String json = gson.toJson(obj);
        editor.putString(key, json);
        editor.commit();
    }

    public LoginModel getLoginModel(String key)
    {
        Gson gson = new Gson();
        String json = sharedpreferences.getString(key, "");
        LoginModel ob= gson.fromJson(json, LoginModel.class);
        return ob;
    }

    public void setCaseListModel(String key, ArrayList<CaseListModel> obj)
    {
        Gson gson = new Gson();
        String json = gson.toJson(obj);
        editor.putString(key, json);
        editor.commit();
    }

    public ArrayList<CaseListModel> getCaseListModel(String key)
    {
        Gson gson = new Gson();
        String json = sharedpreferences.getString(key, "");
        Type type = new TypeToken<ArrayList<CaseListModel>>() {}.getType();
        ArrayList<CaseListModel> arrayList = gson.fromJson(json, type);
        return arrayList;
    }

    public int getInt(String key, int val) {
        return sharedpreferences.getInt(key,val);
    }

}

No comments:

Post a Comment