Domanda

Can anyone help me identify the issue with the following. I have a class that manages user login. When user provides correct username and password I get their details from web service which includes an array of string variables that contains phone numbers. The class is as follows

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();


    }

In the activity class When I try to get Contact1 like below I always get null, but for other values it is working (Age, gender, etc).

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

I faced same issue for getting the age from sharedpreference before. I managed it to get it work by making the following change in the SessionManager class

I changed this line of code

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

To this

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

I removed the quotes from key name and it worked. I don't know how to make it work inside the loop. I have debugged the code step by step and I noticed the values from the array is indeed getting added without any errors inside the loop. Am I missing something here? Any help would be appreciated. Thanks in advance.

UPDATE Here is my User entity class

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;
    }
}
È stato utile?

Soluzione

Try this way

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

Remove "" from preference and directly provide preference name

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

Update: try this way:

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

Altri suggerimenti

very simple, your key variable name is KEY_CONTACT1 and its value is contact1

so when you add value to pref, you use KEY_CONTACT1 as key name which is wrong

at the for loop change to this

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

When You try to get Contact1, remove

SessionManager sm = new SessionManager(getApplicationContext());

and add below code:

pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);

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

Hope this will solve your error.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top