SharedPreference添加值不起作用

时间:2014-05-07 05:54:09

标签: android arraylist sharedpreferences android-preferences

任何人都可以帮我识别以下问题。我有一个管理用户登录的课程。当用户提供正确的用户名和密码时,我会从Web服务获取其详细信息,其中包含一组包含电话号码的字符串变量。该课程如下

SessionManager.Class

public class SessionManager {
    // Shared Preferences
    SharedPreferences pref;

    // Editor for Shared preferences
    Editor editor;

    // Context
    Context _context;

    // Shared pref mode
    int PRIVATE_MODE = 0;

    // Sharedpref file name
    private static final String PREF_NAME = "LoginPref";

    // All Shared Preferences Keys
    private static final String IS_LOGIN = "IsLoggedIn";

    public static final String KEY_EMAIL = "email";

    public static final String KEY_GENDER = "gender";

    public static final String KEY_AGE = "age";

    public static final String KEY_CONSULTANT = "consultant";

    public static final String KEY_CONTACT1 = "contact1";

    public static final String KEY_CONTACT2 = "contact2";

    public static final String KEY_CONTACT3 = "contact3";

    public static final String KEY_CONTACT4 = "contact4";

    public static final String KEY_CONTACT5 = "contact5";

    public static final String KEY_CONTACT_SIZE = "contactsize";

    public static final String KEY_NAME = "userfullname";

    // Constructor
    public SessionManager(Context context) {
        this._context = context;
        pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
        editor = pref.edit();
    }

    public void createLoginSession(String email, User aUser) {

        editor.putBoolean(IS_LOGIN, true);

        editor.putString(KEY_EMAIL, email);
        editor.putBoolean(KEY_CONSULTANT, aUser.getConsultantPreference());
        editor.putString(KEY_NAME, aUser.getName());
        editor.putInt(KEY_AGE, aUser.getAgeGroupID());
        editor.putInt(KEY_GENDER, aUser.getGenderID());
        editor.putInt(KEY_CONTACT_SIZE, aUser.getContacts().size());

//loop through arraylist and add values
        for(int i=0;i<aUser.getContacts().size();i++){
            editor.putString("KEY_CONTACT"+(i+1), aUser.getContacts().get(i));
        }

        editor.commit();
    }

// returns contact number 1
    public String getContact1() {

        return pref.getString(KEY_CONTACT1, null);
    }


//returns age
    public int getAge() {

        return pref.getInt(KEY_AGE, 0);
    }

    public void logoutUser(){
        // Clearing all data from Shared Preferences
        editor.clear();
        editor.commit();


    }

在活动课程中当我尝试获得如下的Contact1时,我总是得到null,但是对于其他值,它正在工作(年龄,性别等)。

SessionManager sm = new SessionManager(getApplicationContext());
int ageGroup = sm.getAge(); // returns expected value
        String contact1 = sm.getContact1(); //contact1 is always null

我之前面临同样的问题,即从共享偏好中获取年龄。我通过在SessionManager

中进行以下更改来管理它以使其工作

我改变了这行代码

editor.putInt("KEY_AGE", aUser.getAgeGroupID()); //used to return 0 the default value

到此

editor.putInt(KEY_AGE, aUser.getAgeGroupID()); //returns expected value

我从密钥名称中删除了引号,但它确实有效。我不知道如何让它在循环中工作。我已经逐步调试了代​​码,我注意到数组中的值确实在循环中没有任何错误。我在这里错过了什么吗?任何帮助,将不胜感激。提前谢谢。

更新这是我的用户实体类

public class User {
    private String fullName;
    private int genderID;
    private int ageGroupID;
    private ArrayList<String> contacts;
    private boolean isPreferFemale;

    public User(String name, int gender, int age,
            ArrayList<String> emergencyContacts, boolean isFemalpreferred) {
        fullName = name;
        genderID = gender;
        ageGroupID = age;
        contacts = emergencyContacts;
        isPreferFemale = isFemalpreferred;
    }

    public User(){}

    public String getName(){
        return fullName;
    }

    public int getGenderID(){
        return genderID;
    }

    public int getAgeGroupID(){
        return ageGroupID;
    }

    public ArrayList<String> getContacts(){
        return contacts;
    }

    public boolean getConsultantPreference(){
        return isPreferFemale;
    }
}

3 个答案:

答案 0 :(得分:1)

试试这种方式

   for(int i=0;i<aUser.getContacts().size();i++){
        editor.putString(KEY_CONTACT+(i+1), aUser.getContacts().get(i));
    }

""移除preference并直接提供preference名称

editor.putString("KEY_CONTACT"+(i+1), aUser.getContacts().get(i)); //remove "" from "KEY_CONTACT" and directly provide preference name like KEY_CONTACT

更新:尝试这种方式:

for(int i=0;i<aUser.getContacts().size();i++){
        editor.putString("contact"+(i+1), aUser.getContacts().get(i));
    }

答案 1 :(得分:1)

很简单, 您的密钥变量名称是KEY_CONTACT1,其值为contact1

所以当你向pref添加值时,你使用KEY_CONTACT1作为错误的键名

在for循环中更改为

for(int i=0;i<aUser.getContacts().size();i++){
            editor.putString("contact"+(i+1), aUser.getContacts().get(i));
}

答案 2 :(得分:0)

当您尝试获取Contact1时,请删除

SessionManager sm = new SessionManager(getApplicationContext());

并添加以下代码:

pref = _context.getSharedPreferences(PREF_NAME,PRIVATE_MODE);

int i = pref.getInt(KEY_AGE,0);

希望这能解决您的错误。