0

hello im very new to coding in android studio so here is the scenario i have this user that logs in then i get his username i need to store the user's username because i needed to use it on the other pages in my project so what i did was do something like this

 public user getLOGusr(String uname, String pass)
{
    SQLiteDatabase db = this.getReadableDatabase();

    String select = "SELECT uid, username, email FROM " + TABLE_USRS + " WHERE " + KEY_NAME + " = ? AND " + KEY_PASS + " = ?";

    Cursor cursor = db.rawQuery(select, new String[]{uname, pass});

    user usr = new user();
    if(cursor != null && cursor.moveToFirst())
    {
        cursor.moveToFirst();
        usr.setUsrid(Integer.parseInt(cursor.getString(0)));
        usr.setuName(cursor.getString(1));
        usr.seteAddress(cursor.getString(2));

        PreferenceManager pm = PreferenceManager.getDefaultSharedPrefs(this);
        SharedPreferences.Editor edit = pm.edit();
        edit.putString("usrnm", cursor.getString(1));
        edit.commit();

        cursor.close();
    }
    else
    {
        usr = null;
    }

    return usr;
}

this is the function inside my dbhandler the problem is on the .getDefaultSharedPrefspart and the .edit() saying cannot resolve method the stuff im only trying to do is put the username into a session any help? thanks so much in advancve!

BourneShady
  • 885
  • 2
  • 16
  • 32
  • you need to see [Android Shared preferences example](http://stackoverflow.com/questions/23024831/android-shared-preferences-example). Please Try searching things on SO before asking..!! – Janki Gadhiya Jul 09 '16 at 04:40

2 Answers2

0

Setting values in Preference:

SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
edit.putString("usrnm", cursor.getString(1));
edit.commit();

Retrieve data from preference:

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
  String name = prefs.getString("usrnm", "No name defined");//"No name defined" is the default value.
}

Reference here

Community
  • 1
  • 1
madhan kumar
  • 1,476
  • 2
  • 20
  • 34
0

getDefaultSharedPreference takes Context object as parameter. By passing this you are passing reference to your dbhandler and not context.

user3215142
  • 306
  • 2
  • 6