java - Why is only 'else' part getting executed, even if 'if' is true? -
here main activity:
package com.santosh.sampleapp; import android.app.activity; import android.content.intent; import android.os.bundle; import android.widget.button; import android.widget.edittext; import android.widget.textview; import android.widget.toast; import android.view.gravity; import android.view.view; import android.view.view.onclicklistener; public class main extends activity { edittext password_field; textview tv_err; button login; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); password_field = (edittext) findviewbyid(r.id.password_field); tv_err = (textview)findviewbyid(r.id.tv_err); login = (button)findviewbyid(r.id.login); final string password = password_field.gettext().tostring(); login.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { try { if (password.equals("password")) { tv_err.settext("good pass"); } else { tv_err.settext("wrong pass"); } } catch (exception e) { tv_err.settext(e.tostring()); } } }); } }
even if type password
@ textview, shows me wrong pass
message.
here src/layout/main.xml if helps:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <edittext android:id="@+id/password_field" android:hint="@string/password_hint" android:inputtype="textpassword" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <button android:id="@+id/login" android:text="@string/get_message" android:layout_height="wrap_content" android:layout_width="wrap_content" /> <textview android:id="@+id/tw_err" android:layout_height="wrap_content" android:layout_width="wrap_content" /> </linearlayout>
because password
not updated. contains original value. update so:
if (password_field.gettext().tostring().equals("password")) {
btw: keep on using final
keyword as possible! minimize mutable state in app , you'll happy man.
Comments
Post a Comment