Reputation: 1627
I'm having trouble saving String-values that resides within the edittext
.
What happens is, the dialog
shows, with edittext
, an ok and a cancel-button.
When the OK button
is pushed, what i want to happen is for the bar
-variable to get the string-value
from the edittext
.
public void dialog(){
final Dialog dialog = new Dialog(myClass.this);
dialog.setContentView(R.layout.mydialog);
dialog.setTitle("I'm soo smart. S-M-R-T. 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 onto this please?
EDIT: This is sample code. Actual code does not have duplicates names on variables.
2ND EDIT: removed duplicates..
Upvotes: 4
Views: 2674
Reputation: 2502
I am assuming this bar is a String type variable, the problem here seem that you are declaring two edittexts by same name, And String variables by same name too, I suggest you to 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);
Upvotes: -2
Reputation: 830
I had similar problems. I made 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();
});
Upvotes: 0
Reputation: 7879
Check if barText
is null.
What happens if you declare it out of the onClick
listener?
Maybe change:
final EditText barText= (EditText) textEntryView.findViewById(R.id.dialog_FOO);
to:
final EditText barText= (EditText) dialog.findViewById(R.id.dialog_FOO);
Upvotes: 3
Reputation: 89
firstly two different edittext boxes must have different id. secondly there will two string variables to store 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);
Upvotes: 0