HashMap保存并获取共享首选项

时间:2016-07-19 14:44:32

标签: android android-sharedpreferences

我有一个项目,我想将在线用户可用的每个变量保存到共享首选项。这是他们的字符串值user_id,Fname,Lname,以及他们的int值“lexile”,“user_id”。我到目前为止一直在线跟踪教程这么好,但它只提供保存两个变量,所以我即兴创作。我在运行时没有遇到任何错误,但在实际使用应用程序时我似乎无法登录并打开第二个活动,可能是因为我的hashmaping。

以下是代码 这是相关登录的一部分

 Response.Listener<String> responseListener = new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        //SUBUKAN NA ISINGIT DINE ANG PAGCHECK SA INTERNET SERCVICE
                        try {
                            JSONObject jsonResponse = new JSONObject(response);
                            boolean success = jsonResponse.getBoolean("success");
                            //OPENS THE NEW ACTIVITY
                            if (success) {
                                String Fname = jsonResponse.getString("Fname");
                                String Lname = jsonResponse.getString("Lname");
                                int lexile = jsonResponse.getInt("lexile");
                                int user_id = jsonResponse.getInt("user_id");
                               // String username = jsonResponse.getString("username");
                              //  int user_id = jsonResponse.getInt("user_id");

                                Intent intent = new Intent(Login.this, User_nav.class);
                                intent.putExtra("Lname", Lname);
                                intent.putExtra("Fname", Fname);
                                intent.putExtra("lexile", lexile);
                                intent.putExtra("user_id", user_id);
                               //    intent.putExtra("username",username);
                                //intent.putExtra("user_id", user_id);

                                //THIS IS THE PART OF THE SESSION MANAGER
                                session.createLoginSession(Lname, Fname, lexile, user_id);
                                Login.this.startActivity(intent);

                                progress.dismiss();
                            }

这是第二项活动,即相关的代码

 SessionManager session;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user);

    session = new SessionManager(getApplicationContext());
    Toast.makeText(getApplicationContext(), "User Login Status: " + session.isLoggedIn(),Toast.LENGTH_LONG).show();
    session.checkLogin();

    HashMap<String, String> user = session.getUserDetails();
    String Lname = user.get(SessionManager.KEY_FNAME);
    String Fname = user.get(SessionManager.KEY_LNAME);

    HashMap<String, Integer> user_int = session.getUserLexileNID();
    Integer lexile = user_int.get(SessionManager.KEY_LEXILE);
    Integer user_id = user_int.get(SessionManager.KEY_USER_ID);

    TextView Name = (TextView) header.findViewById(R.id.Name);
    TextView displayLexile = (TextView) header.findViewById(R.id.lexile);

    Name.setText(Html.fromHtml(Lname+", " +Fname));
    displayLexile.setText("Lexile Level: "+lexile + "" +user_id);

这是SessionManager,它包含所有用于会话制作的代码。

package com.capstone.jmilibraryapp;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.content.SharedPreferencesCompat;
import java.util.HashMap;

public class SessionManager {
SharedPreferences pref;
SharedPreferences.Editor editor;
Context _context;
int PRIVATE_MODE = 0;
private static final String PREF_NAME = "JMIPref";
private static final String IS_LOGIN = "IsLoggedIn";
public static final String KEY_FNAME = "Fname";
public static final String KEY_LNAME = "Lname";
public static final String KEY_USER_ID = "user_id";//PART OF THE PROBLEM
public static final String KEY_LEXILE = "lexile";///PART OF THE PROBLEM



//constructor
public SessionManager(Context context){
    this._context = context;
    pref =_context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
    editor = pref.edit();
}
//create login session
public void createLoginSession(String Fname, String Lname, Integer user_id, Integer lexile){
    editor.putBoolean(IS_LOGIN, true);
    editor.putString(KEY_FNAME, Fname);
    editor.putString(KEY_LNAME, Lname);
    editor.putInt(KEY_USER_ID, user_id);
    editor.putInt(KEY_LEXILE, lexile);
    editor.commit();
}
//Part of the tutorial dont seem to have any problem
public HashMap<String, String> getUserDetails(){
    HashMap<String, String> user = new HashMap<String, String>();
    user.put(KEY_FNAME, pref.getString(KEY_FNAME, null));
    user.put(KEY_LNAME, pref.getString(KEY_LNAME, null));
    return user;
}
//THIS MAYBE THE SOURCE OF THE PROBLEM
public HashMap<String, Integer> getUserLexileNID(){
    HashMap<String, Integer> user_int = new HashMap<>();
    user_int.put(KEY_LEXILE, pref.getInt(KEY_LEXILE,-1 ));
    user_int.put(KEY_USER_ID, pref.getInt(KEY_USER_ID,-1 ));
    return user_int;
}
public void checkLogin(){
    if (!this.isLoggedIn()) {

    Intent i = new Intent(_context, Login.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    _context.startActivity(i);
    }}
public void logoutUser(){

    editor.clear();
    editor.commit();
    Intent i = new Intent(_context, Login.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    _context.startActivity(i);
}
public boolean isLoggedIn(){
    return pref.getBoolean(IS_LOGIN, false);
}
}

0 个答案:

没有答案
相关问题