How can i do if i want user and admin display different layout after login?

时间:2017-12-18 07:04:46

标签: javascript java sql json sql-server-2008

My mobile application got 2 account types of users which are admin and user. How can I do if I want user and admin display different layout after login? This is my login activity. Someone can help me, please? I'm a beginner. Thanks. Or anyone got any link that guide beginner to do these also can post it at here. Pretty much thanks for everyone.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    connectionClass = new ConnectionClass();
    edtuserid = (EditText) findViewById(R.id.edtuserid);
    edtpass = (EditText) findViewById(R.id.edtpass);
    btnlogin = (Button) findViewById(R.id.btnlogin);
    pbbar = (ProgressBar) findViewById(R.id.pbbar);
    pbbar.setVisibility(View.GONE);

    shp = this.getSharedPreferences("UserInfo", MODE_PRIVATE);

    String userid = shp.getString("UserId", "none");

    if (userid.equals("none") || userid.trim().equals("")) {

    } else {

        Intent i = new Intent(LoginActivity.this, MainActivity.class);
        startActivity(i);
        finish();

    }


    btnlogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DoLogin doLogin = new DoLogin();
            doLogin.execute("");
        }
    });

}

public class DoLogin extends AsyncTask<String,String,String>
{
    String z = "";
    Boolean isSuccess = false;
    String userid = edtuserid.getText().toString();
    String password = edtpass.getText().toString();

    @Override
    protected void onPreExecute() {
        pbbar.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onPostExecute(String r) {
        pbbar.setVisibility(View.GONE);
        Toast.makeText(LoginActivity.this,r,Toast.LENGTH_SHORT).show();
        if(isSuccess) {
            Intent i = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(i);
            finish();
        }
    }

    @Override
    protected String doInBackground(String... params) {
        if(userid.trim().equals("")|| password.trim().equals(""))
            z = "Please enter User Id and Password";
        else
        {
            try {
                Connection con = connectionClass.CONN();
                if (con == null) {
                    z = "Error in connection with SQL server";
                } else {
                    String query = "select * from dbo.demo where UserId='" + userid + "' and Password='" + password + "'";
                    Statement stmt = con.createStatement();
                    ResultSet rs = stmt.executeQuery(query);

                    if(rs.next())
                    {

                        z = "Login successfull";
                        isSuccess=true;
                    }
                    else
                    {
                        z = "Invalid Credentials";
                        isSuccess = false;
                    }

                }
            }
            catch (Exception ex)
            {
                isSuccess = false;
                z = "Exceptions";
            }
        }
        return z;
    }
}

1 个答案:

答案 0 :(得分:0)

I did this kind of project once. To overcome this login issue, we added a column in our database as "role" which contains role as admin,user and etc. Create 2 different pages for admin and user. After checking the login credentials check the role also then redirect accordingly.