• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Allow admin to login to a separate screen and normal users to a different screen.

Pooveshin

Newbie
Hi there

I have developed this android app and need assistance with the Login. There are normal users and one Admin user, the Admin user goes to a different screen (Admin_Menu.class) and the normal users go to (User_Menu.class), how can I change my code to allow for this. My code is listed below.

Databasehelper
Code:
public class DatabaseHelper extends SQLiteOpenHelper
{

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "contacts.db";
    private static final String TABLE_NAME = "contacts";
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_NAME = "name";
    private static final String COLUMN_EMAIL = "email";
    private static final String COLUMN_UNAME = "uname";
    private static final String COLUMN_PASS = "pass";
    SQLiteDatabase db;

    private static final String TABLE_CREATE = "Create table contacts (id integer primary key not null, "+
    "name text not null , email text not null , uname text not null , pass text not null);";

    public DatabaseHelper (Context context)
    {
        super(context , DATABASE_NAME , null , DATABASE_VERSION);
    }

    [USER=1021285]@override[/USER]
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(TABLE_CREATE);
        this.db = db;

    }

    public void insertContact(Contact c)
    {
        db = this.getWritableDatabase();
        ContentValues values = new ContentValues();

        String query = "SELECT * FROM contacts";
        Cursor cursor = db.rawQuery(query , null);
        int count = cursor.getCount();

        values.put(COLUMN_ID , count);
        values.put(COLUMN_NAME , c.getName());
        values.put(COLUMN_EMAIL , c.getEmail());
        values.put(COLUMN_UNAME , c.getUname());
        values.put(COLUMN_PASS , c.getPass());

        db.insert(TABLE_NAME , null , values);
        db.close();
    }

    public String searchPass(String uname)
    {
        db = this.getReadableDatabase();
        String query = "SELECT uname, pass FROM " + TABLE_NAME;
        Cursor cursor = db.rawQuery(query , null);
        String a, b;
        b = "Not Found";
        if (cursor.moveToFirst())
        {
            do {
                a = cursor.getString(0);

                if (a.equals(uname))
                {
                    b = cursor.getString(1);
                    break;
                }
            }
            while(cursor.moveToNext());
        }
        return b;
    }

    [USER=1021285]@override[/USER]
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String query = "DROP TABLE IF EXISTS " + TABLE_NAME;
        db.execSQL(query);
        this.onCreate(db);
    }

}
Login class
Code:
public class LoginActivity extends AppCompatActivity{

    DatabaseHelper helper = new DatabaseHelper(this);

    [USER=1021285]@override[/USER]
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login2);
    }

    public void onButtonClick (View view)
    {
        if (view.getId() == R.id.Blogin)
        {
            EditText a = (EditText)findViewById(R.id.TFusername);
            String str = a.getText().toString();

            EditText b = (EditText)findViewById(R.id.TFpassword);
            String pass = b.getText().toString();

            String password = helper.searchPass(str);
            if (pass.equals(password)) {

                Intent detailIntent = new Intent(this, User_Menu.class);
                detailIntent.putExtra("Username", str);
                startActivity(detailIntent);
            }
            else
            {
                Toast.makeText(this, "Incorrect Login details", Toast.LENGTH_SHORT).show();
            }
        }

        if (view.getId() == R.id.BsignUp)
        {
            Intent detailIntent = new Intent(this, SignUp.class);
            startActivity(detailIntent);
        }
    }
}
 
I would do this by adding a ROLE column to the database. This would have a value of "Admin" or "User".
Add a property called role to your Contact class.
Make searchPass() method return a Contact, instead of a String.
 
Back
Top Bottom