In a dialog layout, I create a second EditText programmatically depending on user choices. When I want to return the inputs, I need to know if a second EditText has been created or not. And I don’t understand how to make this check. My statement if (edittextTwo != null) is always null, even when the second EditText has been displayed and entered text into.
Here are the methods which create the second EditText and return their inputs:
if (edittextTwo != null)
private void displayASecondEdittext(String title) {
ViewGroup layout = (ViewGroup) findViewById(R.id.layout_editdialog);
TextView titleTwo = new TextView(this);
titleTwo.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
titleTwo.setText(title);
layout.addView(titleTwo);
EditText edittextTwo = new EditText(this);
edittextTwo.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
layout.addView(edittextTwo);
}//END displayASecondInputfield
private void returnResult() {
Intent resultIntent = new Intent(this, EditDialog.class);
resultIntent.putExtra(EDITONE, edittextOne.getText().toString());
if (edittextTwo != null) {
resultIntent.putExtra(EDITTWO, edittextTwo.getText().toString());
Log.v(TAG, "edittextTwo ="+edittextTwo.getText().toString());
}
setResult(Activity.RESULT_OK, resultIntent);
finish();
}//END returnResult
move
EditText edittextTwo;out side of your method. put it here:And then, in your
DisplayASecondEditText(), do this instead of what you have:This will make your declaration global, and allow all all methods to access the variable.