Apology for another newbie question. I have this block of code in my MainActivity should check the users and returns an appropriate activity based on the if else statement. Problem is that it skips the if block and I can’t figure out why. All I know is that the getID function works fine on other classes. Thank you for reading this.
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();
}
}
}
UserFunction Class
/* 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;
}
DatabaseHandler Class
/* 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;
}
Your getID() method always returns the ID of the last user in the database. This user is not id==”100″ since your if statement is evaluating to false.
In getUserID, you select all records in the login table. Then:
if there are any users, this will return the ID of the last one, if there are no users, it will return “”.