dimanche 21 juin 2015

Intent inside if statement in android not working

I am creating a simple login form in android using SQLite as database. I have this if statement that checks whether the password entered is equal to the stored password in the database. If both passwords are the same, it will redirect to another page. However, the intent is not working. Please help me out. These are my codes:

Login.java

public class Login extends AppCompatActivity {

EditText uname, pass;
Button login;
TextView register;

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

    uname = (EditText)findViewById(R.id.et_uname);
    pass = (EditText) findViewById(R.id.et_pass);
    register = (TextView) findViewById(R.id.txtbtn_reg);
    login = (Button) findViewById(R.id.btn_login);

    register.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent   a = new Intent (getBaseContext(), Register.class); 
            startActivity(a);
        }
    });
}

public void lookupUser(View view) {
    MYDBHandler dbHandler = new MYDBHandler(this, null, null, 1);

    UserInformation user = dbHandler.searchUser(uname.getText().toString());

    if (user != null) {
        String userpass = String.valueOf(user.getPassword());


        if (userpass == pass.getText().toString())
        {
            Toast.makeText(this,"Welcome",Toast.LENGTH_LONG).show();
            Intent i = new Intent (Login.this, Main.class); // it should redirect to the view but it's not working

            startActivity(i);

        }



    } else {
        new AlertDialog.Builder(this)
                .setTitle("Notification")
                .setMessage("Please input data")
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                }).show();
                uname.setText("");
                pass.setText("");
    }
}

activity_login.xml

 <LinearLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".Main"
android:weightSum="1"
android:orientation="vertical"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="30dp"
android:background="#FFFFFF">

<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/et_uname"
    android:hint="@string/username"
    android:layout_marginTop="90dp"
    android:layout_marginBottom="10dp"
    android:textColor="#EF6C00"
    android:textAlignment="center"
    android:autoText="false"
    android:background="@color/edittext_background"
    android:padding="10dp" />

<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="textPassword"
    android:hint="@string/password"
    android:id="@+id/et_pass"
    android:layout_marginBottom="10dp"
    android:textColor="#EF6C00"
    android:textAlignment="center"
    android:background="@color/edittext_background"
    android:padding="10dp"
    android:textIsSelectable="false" />

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/go"
    android:id="@+id/btn_login"
    android:background="@color/button_background"
    android:textColor="#ffffffff"
    android:onClick="lookupUser" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="@string/register"
    android:id="@+id/txtbtn_reg"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="10dp"
    android:textColor="#9E9E9E"
    android:textStyle="bold|normal" />

Aucun commentaire:

Enregistrer un commentaire