正确使用session来存储变量android studio

时间:2016-07-09 04:28:44

标签: android

你好我是非常新的android工作室编码所以这里是我有这个用户登录然后我得到他的用户名我需要存储用户的用户名,因为我需要在我的项目中的其他页面使用它所以我做的是做这样的事情

 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;
}

这是我的dbhandler中的函数,问题出在.getDefaultSharedPrefs部分,.edit()说无法解决方法,我只想尝试做的事情是将用户名放入会话任何帮助?非常感谢advancve!

2 个答案:

答案 0 :(得分:0)

在首选项中设置值:

Warning (from warnings module):
  File "C:\Python34\lib\site-packages\sklearn\preprocessing\imputation.py", line 347
    "observed values: %s" % missing)
UserWarning: Deleting features without observed values: [2 6]
array([[-1.284658  ,  0.246541  , -1.120987  ,  0.559911  , -1.18987   ],
       [ 0.13254586,  0.430597  , -0.13067843, -1.29208   ,  0.00710114],
       [ 1.418761  , -0.004749  , -0.181932  , -0.305756  , -0.13587   ],
       [ 0.13254586, -0.376318  , -0.13067843,  0.074135  ,  0.00710114],
       [-0.019873  ,  0.00621   ,  0.364384  ,  1.029895  , -0.188727  ],
       [ 0.903661  ,  0.123575  , -0.55697   ,  1.344985  , -1.109806  ],
       [-0.069168  , -0.385597  ,  0.684345  ,  0.64592   ,  1.159898  ],
       [ 0.13254586,  0.030239  , -0.13067843, -0.037102  ,  0.00710114],
       [-0.256409  ,  0.106735  , -0.72971   ,  0.254626  ,  1.064925  ],
       [ 0.235507  , -0.087767  ,  0.626121  ,  1.391286  ,  0.449158  ]])

从偏好中检索数据:

np.reshape(np.in1d(imp.transform(df), imp.statistics_), imp.transform(df).shape)
array([[False, False, False, False, False],
       [ True, False,  True, False,  True],
       [False, False, False, False, False],
       [ True, False,  True, False,  True],
       [False, False, False, False, False],
       [False, False, False, False, False],
       [False, False, False, False, False],
       [ True, False,  True, False,  True],
       [False, False, False, False, False],
       [False, False, False, False, False]], dtype=bool)

Reference here

答案 1 :(得分:0)

getDefaultSharedPreference将Context对象作为参数。通过传递,您将传递对dbhandler的引用,而不是上下文。