我尝试过以下代码,我试图在注销应用程序之前创建用户会话。问题是我正在尝试将用户信息发送到页面profile.java
。我尝试使用intent extras但它失败了。请提出任何建议。
login.java
public class login extends AppCompatActivity {
private FunTubeOperations FunTubeDBOperations;
private DatabaseWrapper db;
private SessionManager session;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
final AlertDialog ad = new AlertDialog.Builder(this).create();
Button login = (Button) findViewById(R.id.login);
Button reg = (Button) findViewById(R.id.reg);
FunTubeDBOperations = new FunTubeOperations(this);
FunTubeDBOperations.open();
// SQLite database handler
db = new DatabaseWrapper(getApplicationContext());
// Session manager
session = new SessionManager(getApplicationContext());
// Check if user is already logged in or not
if (session.isLoggedIn()) {
// User is already logged in. Take him to main activity
Intent intent = new Intent(login.this, profile.class);
//intent.putExtra("name", user);
startActivity(intent);
finish();
}
try {
login.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
EditText email = (EditText) findViewById(R.id.email);
EditText password = (EditText) findViewById(R.id.password);
if (email != null && password !=null) {
String em=email.getText().toString();
String pwd=password.getText().toString();
String user= FunTubeDBOperations.loginUsers(em, pwd);
if(user!=null)
{
session.setLogin(true);
Intent intent = new Intent(login.this, profile.class);
intent.putExtra("name", user);
startActivity(intent);
finish();
}}
});
} catch (Exception er) {
ad.setMessage(er.toString());
}
}
}
DataBaseWrapper.java
public class DatabaseWrapper extends SQLiteOpenHelper {
private static final String TAG = DatabaseWrapper.class.getSimpleName();
//For user table
public static final String FUNTUBE = "User";
public static final String FUNTUBE_ID = "_id";
public static final String FUNTUBE_UNAME = "_username";
public static final String FUNTUBE_EMAIL = "_email";
public static final String FUNTUBE_PASSWORD = "_password";
public static final String FUNTUBE_FNAME = "_fname";
public static final String FUNTUBE_LNAME = "_lname";
public static final String FUNTUBE_PHONE = "_phone";
public static final String FUNTUBE_COUNTRY = "_country";
public static final String FUNTUBE_GENDER = "_gender";
public static final String FUNTUBE_VIDEOPATH = "_videopath";
public static final String FUNTUBE_BDAY = "_bday";
public static final String FUNTUBE_YEAR = "_year";
public static final String FUNTUBE_INTEREST = "_interest";
public static final String FUNTUBE_RELIGION = "_religion";
public static final String FUNTUBE_ABOUT = "_about";
public static final String FUNTUBE_QUOTE = "_qoute";
public static final String FUNTUBE_JOB = "_job";
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
private static final String DATABASE_NAME = "FunTubeApplication.db";
private static final int DATABASE_VERSION = 1;
// creation SQLite statement
private static final String DATABASE_CREATE = "create table " + FUNTUBE
+ "(" + FUNTUBE_ID + " integer primary key autoincrement, "
+ FUNTUBE_UNAME + " text not null,"
+ FUNTUBE_EMAIL + " text not null,"
+ FUNTUBE_PASSWORD + " text not null,"
+ FUNTUBE_FNAME + " text not null,"
+ FUNTUBE_LNAME + " text not null,"
+ FUNTUBE_PHONE + " text not null,"
+ FUNTUBE_COUNTRY + " text not null,"
+ FUNTUBE_GENDER + " text not null,"
+ FUNTUBE_VIDEOPATH + " text not null,"
+ FUNTUBE_BDAY + " text not null,"
+ FUNTUBE_YEAR + " text not null,"
+ FUNTUBE_INTEREST + " text not null,"
+ FUNTUBE_RELIGION + " text not null,"
+ FUNTUBE_ABOUT + " text not null,"
+ FUNTUBE_QUOTE + " text not null,"
+ FUNTUBE_JOB + " text not null)";
public DatabaseWrapper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (newVersion > oldVersion) {
db.execSQL("DROP TABLE IF EXISTS " + FUNTUBE);
db.execSQL("DROP TABLE IF EXISTS " + FUNTUBE_UPLOADFILE);
onCreate(db);
}
}
/**
* Getting user data from database
*/
public HashMap<String, String> getUserDetails() {
HashMap<String, String> user = new HashMap<String, String>();
String selectQuery = "SELECT * FROM " + FUNTUBE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
if (cursor.getCount() > 0) {
user.put("_username", cursor.getString(1));
user.put("_email", cursor.getString(2));
user.put("_password", cursor.getString(3));
user.put("_fname", cursor.getString(4));
user.put("_lname", cursor.getString(5));
user.put("_phone", cursor.getString(6));
user.put("_country", cursor.getString(7));
user.put("_gender", cursor.getString(8));
user.put("_videopath", cursor.getString(9));
user.put("_bday", cursor.getString(10));
user.put("_year", cursor.getString(11));
user.put("_interest", cursor.getString(12));
user.put("_religion", cursor.getString(13));
user.put("_about", cursor.getString(14));
user.put("_quote", cursor.getString(15));
user.put("_job", cursor.getString(16));
}
cursor.close();
db.close();
// return user
Log.d(TAG, "Fetching user from Sqlite: " + user.toString());
return user;
}
}
答案 0 :(得分:0)
可以使用你的方式,但我还有其他可以帮助你的东西。
做一件事创建公共静态变量并在此变量中设置数据。在您的配置文件类之后,使用此变量,如全局变量。
像 方式1。
login.java
public static String UserName = null;
// set user name after login in above variable
UserName = "tempUser";
// using intent go to profile page without passing any extra data
// before call intent check that UserName should not be null;
profile.java
// get user name
TextView username;
username.setText(Login.UserName);
方式2
优先存储数据.. 设置首选项数据表单登录并从配置文件中获取首选项数据。