vendredi 22 janvier 2016

Android Studio login function

I am new for Android Studio...So I had faced some problem in login part. I have two type of user membership. One is "B" as buyer and another is "S" as seller. When membership "B" login, will go buyer interface while membership "S" will go seller interface. But I had no idea how to do it...Can anyone teach me?

Here is my login database code

 public String getInformation(String username){
        SQLiteDatabase SQ = this.getReadableDatabase();
        String query = "select Username,Password,membership from "+ Table_NAME;
        Cursor CR = SQ.rawQuery(query,null);
        String a,b,c;
        b = "not found";

        if (CR.moveToFirst())
        {
            do {
                a = CR.getString(0);

                if (a.equals(username)) {
                    b = CR.getString(1);
                    c = CR.getString(1);
                    break;
                }
            }while (CR.moveToNext());
        }
        return b;

    }

Here is my login code

    package com.example.user.register;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class Login extends AppCompatActivity {
    Button login;
    EditText USER_NAME,USER_PASS;
    String username,password,membership;
    DatabaseOperations db = new DatabaseOperations(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        login = (Button) findViewById((R.id.login));

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    if(v.getId() == R.id.login)
                    {
                        USER_NAME = (EditText) findViewById(R.id.etUsername);
                        USER_PASS = (EditText) findViewById(R.id.etPassword);
                        username = USER_NAME.getText().toString();
                        password = USER_PASS.getText().toString();

                        password = db.getInformation(username);
                        if(password.equals(password)) {
                            if (membership.equals("S")) {
                                Toast.makeText(getBaseContext(), "Login Successful" + username, Toast.LENGTH_LONG).show();
                                Intent i = new Intent(Login.this, Hello.class);
                                startActivity(i);
                                finish();
                            }
                            else
                            {
                                Toast.makeText(getBaseContext(), "Login Successful" + username, Toast.LENGTH_LONG).show();
                                Intent i = new Intent(Login.this, yoyo.class);
                                startActivity(i);
                                finish();
                            }
                        }
                    else
                    {
                        Toast.makeText(getBaseContext(), "Wrong Username or Password",Toast.LENGTH_LONG).show();
                        USER_NAME.setText("");
                       USER_PASS.setText("");
                    }
                }
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_login, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Aucun commentaire:

Enregistrer un commentaire