How to get value from EditText in AlertDialog?

When you select the context menu option, AlertDialog appears. I want the user to enter text in EditText in AlertDialog, and when the user clicks PositiveButton, the value of the EditText can be "returned" to the main method. Here is the relevant code from my class:

public class PassPlay extends ListActivity { public static final int PENALTY_ID = Menu.FIRST+1; public static final int FUMBLE_ID = Menu.FIRST+2; public static final int ADDLYDS_ID = Menu.FIRST+3; public static final int SAFETY_ID = Menu.FIRST+4; EditText ydsFromAlertDialog; String penYdsStr; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.passplay); ydsFromAlertDialog=(EditText)findViewById(R.id.passYdsLabel); registerForContextMenu(getListView()); } public boolean onCreateOptionsMenu(Menu menu) { populateMenu(menu); return(super.onCreateOptionsMenu(menu)); } public boolean onOptionsItemSelected(MenuItem item) { return(applyMenuChoice(item) || super.onOptionsItemSelected(item)); } public boolean onContextItemSelected(MenuItem item) { return(applyMenuChoice(item) || super.onContextItemSelected(item)); } private void populateMenu(Menu menu) { menu.add(Menu.NONE, PENALTY_ID, Menu.NONE, "Penalty"); menu.add(Menu.NONE, FUMBLE_ID, Menu.NONE, "Fumble"); menu.add(Menu.NONE, ADDLYDS_ID, Menu.NONE, "Additional Yards"); menu.add(Menu.NONE, SAFETY_ID, Menu.NONE, "Safety"); } private boolean applyMenuChoice(MenuItem item) { LayoutInflater factory = LayoutInflater.from(this); final View textEntryView; switch (item.getItemId()) { case PENALTY_ID: textEntryView = factory.inflate(R.layout.textdialog, null); new AlertDialog.Builder(this) .setView(textEntryView) .setIcon(android.R.drawable.ic_dialog_alert) .setTitle(R.string.timeout) .setPositiveButton(R.string.offense, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //Actions for offensive timeout EditText penaltyYds=(EditText)findViewById(R.id.ydsAssessedLabel); penYdsStr = penaltyYds.getText().toString(); ydsFromAlertDialog.setText(penYdsStr); } }) .setNeutralButton(R.string.defense, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //Actions for defensive timeout } }) .setNegativeButton(R.string.cancel, null) .show(); return true; case FUMBLE_ID: //Fumble window return true; case ADDLYDS_ID: //Additional Yards window return true; case SAFETY_ID: //Safety window return true; } return(false); } } 

The main XML format (passplay.xml) has your regular text elements, EditTexts, CheckBoxes, etc. I want to set one of these EditTexts (ydsFromAlertDialog) to the value entered in AlertDialog (EditText patentYds). The AlertDialog XML layout (textdialog.xml) is very simple with one TextView and one EditText.

When I run the program, the following line errors: "Application unexpectedly stopped."

 penYdsStr = penaltyYds.getText().toString(); 

So, I want to press the "Penalty" menu button, have an AlertDialog with EditText where I enter the number, and when I click PositiveButton, the value of the EditText ydsFromAlertDialog changes to what was entered in the dialog box.

In fact, I have a database table with 5 columns, 4 of which will be filled with regular fields, but the fifth will be filled with the value entered in the dialog box. I wondered if I could β€œreturn” it to β€œbe with” the rest of the values, I could save it in the same table as the others.

Let me know if you need more information. Thanks!

+4
source share
1 answer

You should get ydsAssessedLabel from a bloated view

 EditText penaltyYds=(EditText)textEntryView.findViewById(R.id.ydsAssessedLabel); 
+4
source

All Articles