Android dialog and edittext values?

I'm having trouble saving String values ​​that are inside edittext .

What happens is a dialog shows, with edittext , ok and a cancel button.

When the OK button clicked, what I want to do is for bar -variable to get the string-value from edittext .

 public void dialog(){ final Dialog dialog = new Dialog(myClass.this); dialog.setContentView(R.layout.mydialog); dialog.setTitle("I'm soo smart. SMRT. Smart."); dialog.setCancelable(true); dialog.show(); Button okButton = (Button) dialog.findViewById(R.id.dialog_OK_BUTTON); okButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { try{ LayoutInflater factory = LayoutInflater.from(Inloggning.this); final View textEntryView = factory.inflate(R.layout.myDialog, null); final EditText barText= (EditText) textEntryView.findViewById(R.id.dialog_FOO); // this gets returned empty. bar= barText.getText().toString(); System.out.println("foo: "+bar); //call(); dialog.hide(); } catch(Exception e){ // do whatever nessesary. } } }); Button cancelButton = (Button) dialog.findViewById(R.id.dialogbtn_cancel); cancelButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { dialog.dismiss(); } }); } 

Can someone shed some light on this, please?

EDIT: This is sample code. Actual code does not have duplicate names for variables.

2ND EDIT: duplicates removed ..

+4
source share
4 answers

Check if barText .

What happens if you declare it from an onClick ?

Subject to change:

 final EditText barText= (EditText) textEntryView.findViewById(R.id.dialog_FOO); 

in

 final EditText barText= (EditText) dialog.findViewById(R.id.dialog_FOO); 
+3
source

firstly, two different edittext blocks must have different identifiers. secondly, there will be two string variables to save them.

final EditText barFirstText = (EditText) textEntryView.findViewById (R.id.dialog1_FOO); final EditText barSecondText = (EditText) textEntryView.findViewById (R.id.dialog2_FOO);

  // these gets returned empty. barFirst= barText.getText().toString(); barSecond= barText.getText().toString(); System.out.println("foo: "+barFirst); System.out.println("foo: "+barSecond); 
0
source

I had similar problems. I made a subclass.

  public class InputDialog extends Dialog{ private String result = null; private Context context = null; private EditText keyEdit = null; public InputDialog(Context _context, String _title, String _message) { super(_context); context = _context; setContentView(R.layout.input_dialog); setTitle(_title); keyEdit = ((EditText) findViewById(R.id.inputEditText)); } public void onBackPressed() { cancel(); } public InputDialog setOkListener(View.OnClickListener _onOk) { findViewById(R.id.okButton).setOnClickListener(_onOk); return this; } public InputDialog setCancelListener(View.OnClickListener _onCancel) { findViewById(R.id.cancelButton).setOnClickListener(_onCancel); return this; } public String getResult() { return keyEdit.getText().toString(); } public EditText getKeyEdit() { return keyEdit; } } 

Using

 inputDialog = new InputDialog(context, getString(R.string.encription_dialog_title), getString(R.string.encription_dialog_message)); inputDialog.setOkListener(new OnClickListener(){ public void onClick(View v) { model.setEncriptionKey(inputDialog.getResult()); listRefresh(); if (inputDialog.getResult() == null || inputDialog.getResult().equals("")) { AppHelper.showMessage(FileManagerActivity.this, getString(R.string.encription_dialog_message)); } else { inputDialog.dismiss(); inputDialog.getKeyEdit().setText(""); } } }); inputDialog.setCancelListener(new OnClickListener(){ public void onClick(View v) { inputDialog.dismiss(); inputDialog.getKeyEdit().setText(""); onBackPressed(); } }); inputDialog.setOnCancelListener(new DialogInterface.OnCancelListener(){ public void onCancel(DialogInterface dialog) { inputDialog.dismiss(); inputDialog.getKeyEdit().setText(""); onBackPressed(); } }); EditText keyEdit = inputDialog.getKeyEdit(); }); 
0
source

I assume that this bar is a variable of type String, the problem here is that you declare two edittexts with the same name, as well as String variables with the same name, I suggest you change the names and try again.

  final EditText barText=(EditText) textEntryView.findViewById(R.id.dialog_FOO); final EditText barText1=(EditText) textEntryView.findViewById(R.id.dialog_FOO1); // these gets returned empty. bar= barText.getText().toString(); bar1= barText.getText().toString(); System.out.println("foo: "+bar); System.out.println("foo: "+bar1); 
-2
source

All Articles