简体   繁体   中英

how to use spinner value to change between activities(android studio)

I'm a beginner with android studio. I'm developing a small app but I faced a problem with the login page I have 3 roles of the user how can log in, I want each one of them to check first if their roles it's in the database then they can go to specific activities (I used spinner for roles). I've tried many times but when I click login it doesn't care for the role I can go to any activity I want !!!!

here's the code: private class login_Api extends AsyncTask<String,String,String> {

    @Override
    protected String doInBackground(String... strings) {
        user_type.getSelectedItem().toString();
        try {
            Connection con = connectionClass.CONN();
            if (con != null) {
                if (user_type.getSelectedItem().equals("Hospital")) {
                    String query = "SELECT * FROM `users` WHERE email = '" + texEmail.getText() + "' and password = '" + texPassword.getText() + "'";
                    Statement stnt = con.createStatement();
                    ResultSet rs = stnt.executeQuery(query);
                    while (rs.next()) {
                        return rs.getString(1);
                    }
                        Intent intent1 = new Intent(LoginActivity.this, HospitalMain.class);
                        startActivity(intent1);
                }
                else if (user_type.getSelectedItem().equals("Donor")) {
                    String query = "SELECT * FROM `users` WHERE email = '" + texEmail.getText() + "' and password = '" + texPassword.getText() + "'";
                    Statement stnt = con.createStatement();
                    ResultSet rs = stnt.executeQuery((query));
                    while (rs.next()) {
                        return rs.getString(1);
                    }
                        Intent intent1 = new Intent(LoginActivity.this, AddHospitalActivity.class);
                        startActivity(intent1);
                }
            }
            else
                Log.d(TAG, "onPreExecute: Error 1");
        }
        catch (Exception e)
        {
            Log.d(TAG, "onPreExecute: Error 2");
            return "error";
        }
        return null;
    }

    @Override
    protected void onPreExecute()
    {
        Toast.makeText(LoginActivity.this, "logging-in ... ", Toast.LENGTH_SHORT).show();
        
    }
    @Override
    protected void onPostExecute(String result) {
        Log.d(TAG, "onPostExecute: "+result);
        if(result!=null) {
            Toast.makeText(LoginActivity.this, "Welcome... ", Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(LoginActivity.this, "Wrong input... ", Toast.LENGTH_SHORT).show();
        }

    }

please help me!!!

The problem is that you aren't storing user_type.getSelectedItem().toString(); in any variable, Try this:

 @Override
protected String doInBackground(String... strings) {
   String userType = user_type.getSelectedItem().toString();
    try {
        Connection con = connectionClass.CONN();
        if (con != null) {
            if (userType.equals("Hospital")) {
                String query = "SELECT * FROM `users` WHERE email = '" + texEmail.getText() + "' and password = '" + texPassword.getText() + "'";
                Statement stnt = con.createStatement();
                ResultSet rs = stnt.executeQuery(query);
                if(rs.hasNext()){
                    finish();
                    Intent intent1 = new Intent(LoginActivity.this, HospitalMain.class);
                    startActivity(intent1);
                }
                //invalid login credentials.
            }
            else if (userType.equals("Donor")) {
                String query = "SELECT * FROM `users` WHERE email = '" + texEmail.getText() + "' and password = '" + texPassword.getText() + "'";
                Statement stnt = con.createStatement();
                ResultSet rs = stnt.executeQuery((query));
                if(rs.hasNext()){
                    finish();
                    Intent intent1 = new Intent(LoginActivity.this, AddHospitalActivity.class);
                    startActivity(intent1);
                }
                //invalid login credentials.
            }
        }
        else
            Log.d(TAG, "onPreExecute: Error 1");
    }
    catch (Exception e)
    {
        Log.d(TAG, "onPreExecute: Error 2");
        return "error";
    }
    return null;
}

@Override
protected void onPreExecute()
{
    Toast.makeText(LoginActivity.this, "logging-in ... ", Toast.LENGTH_SHORT).show();
    
}
@Override
protected void onPostExecute(String result) {
    Log.d(TAG, "onPostExecute: "+result);
    if(result!=null) {
        Toast.makeText(LoginActivity.this, "Welcome... ", Toast.LENGTH_SHORT).show();
    }
    else
    {
        Toast.makeText(LoginActivity.this, "Wrong input... ", Toast.LENGTH_SHORT).show();
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM