为另一个新手问题道歉。我的 MainActivity 中有这段代码应该检查用户并根据 if else 语句返回适当的活动。问题是它跳过了 if 块,我不知道为什么。我所知道的是 getID 函数在其他类上运行良好。谢谢您阅读此篇。
public class MainActivity extends Activity {
UserFunctions userFunctions;
@Override
public void onCreate(Bundle savedInstanceState) {
userFunctions = new UserFunctions();
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
UserFunctions fn = new UserFunctions();
String id = fn.getID(getApplicationContext());
if("100".equals(id)){
Intent in = new Intent(getApplicationContext(), AdminActivity.class);
startActivity(in);
finish();
}else{
Intent in = new Intent(getApplicationContext(), UserActivity.class);
startActivity(in);
finish();
}
}
}
用户函数类
/* Get user ID from DatabaseHandler */
public String getID(Context context) {
String id = "";
DatabaseHandler db = new DatabaseHandler(context);
Cursor cursor = db.getUserID();
if(cursor != null) {
while(cursor.moveToNext()) {
id = cursor.getString(0);
}
} else {
id = "";
}
cursor.close();
db.close();
return id;
}
数据库处理程序类
/* Get user ID from the database */
public Cursor getUserID() {
String qry = "SELECT id FROM " + TABLE_LOGIN;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(qry, null);
return cursor;
}