Reputation: 4662
The code snippet below displays a Dialog with a simple login-form. The problem is that when the user hits the login-button, the text that was entered into the EditTexts are not returned on the getText()-call. However, if I set android:setText="foo" on the EditTexts in the xml-layout "foo" is returned on getText(). Any idea why the text entered at runtime won't stick?
private void showLoginDisplay() {
loginDialog = new Dialog(GOFdroid.this);
loginDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
loginDialog.setTitle("Logga in");
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialogView = li.inflate(R.layout.login_dialog, null);
loginDialog.setContentView(dialogView);
loginDialog.show();
Button loginButton = (Button) dialogView.findViewById(R.id.login_button);
Button cancelButton = (Button) dialogView.findViewById(R.id.cancel_button);
EditText unameView = (EditText) dialogView.findViewById(R.id.uname_id);
if (unameView != null)
Log.d(TAG, "unameView != null");
Log.d(TAG, "uname.getText(): " + unameView.getText().toString());
uname = unameView.getText().toString();
EditText pwdView = (EditText) dialogView.findViewById(R.id.pwd_id);
if (pwdView != null)
pwd = pwdView.getText().toString();
Log.d(TAG, "uname = " + uname + ", pwd = " + pwd);
loginButton.setOnClickListener(new OnClickListener() {
//@Override
public void onClick(View v) {
Log.d(TAG, "Clicked <logga in>");
loginDialog.dismiss();
viewEvents = new Runnable() {
//@Override
public void run() {
getEvents(uname, pwd);
}
};
Thread thread = new Thread(null, viewEvents, "MagentoBackground");
thread.start();
pDialog = ProgressDialog.show(GOFdroid.this,
"Uppdaterar...", "Hämtar registrerade körningar...", true);
}
});
}
and the XML:
<?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"
android:paddingLeft="10dip"
android:paddingRight="10dip"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/uname_label"/>
<EditText
android:id="@+id/uname_id"
android:layout_width="220dip"
android:layout_height="wrap_content"
android:text="foo" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/pwd_label"
android:paddingTop="10dip"/>
<EditText
android:id="@+id/pwd_id"
android:layout_width="220dip"
android:layout_height="wrap_content"
android:text=""
android:inputType="textPassword" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="15dip"
>
<Button
android:id="@+id/login_button"
android:layout_width="100dip"
android:layout_height="fill_parent"
android:text="Logga in" />
<Button
android:id="@+id/cancel_button"
android:layout_width="100dip"
android:layout_height="fill_parent"
android:text="Avbryt" />
</LinearLayout>
</LinearLayout>
Upvotes: 3
Views: 12830
Reputation: 766
The problem is that you are not updating uname and pwd within the anonymous inner class. You only update uname and pwd when the showLoginDisplay() method is called. When the user changes the text, and hits the login button the onClick() method is called.
Within the onClick() method of the inner class you want to do:
uname = unameView.getText();
pwd = pwdView.getText();
You will have to make unameView
and pwdView
global/field variables, so they are accessable by the anonymous inner class.
The reason you have to do this is because when the login button is pressed, only the code with the anonymous inner class's onClick is run.
I hope that fixes the problem.
Upvotes: 7