Reputation: 1127
I try to use a EditText
in a Dialog
. I use the sample from developer.android.com (http://developer.android.com/guide/topics/ui/dialogs.html). Now i want to get the Value
of the EditText when i press the positive Button
.
My Code
public Dialog onCreateDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(main_list.this);
// Get the layout inflater
final LayoutInflater inflater = main_list.this.getLayoutInflater();
builder.setView(inflater.inflate(R.layout.serie_add, null))
// Add action buttons
.setPositiveButton(R.string.search, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
View promptsView = inflater.inflate(R.layout.serie_add, null);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.series);
Intent intent = new Intent();
intent.setClass(main_list.this, add_series.class);
intent.putExtra("name", userInput.getText());
startActivity(intent);
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
return builder.create();
}
Upvotes: 0
Views: 66
Reputation: 43023
Rewrite your code as:
public Dialog onCreateDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(main_list.this);
// Get the layout inflater
final LayoutInflater inflater = main_list.this.getLayoutInflater();
View view = inflater.inflate(R.layout.serie_add, null);
final EditText userInput = (EditText) view
.findViewById(R.id.series);
builder.setView(view)
// Add action buttons
.setPositiveButton(R.string.search, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
View promptsView = inflater.inflate(R.layout.serie_add, null);
Intent intent = new Intent();
intent.setClass(main_list.this, add_series.class);
intent.putExtra("name", userInput.getText().toString());
startActivity(intent);
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
return builder.create();
}
Upvotes: 0
Reputation: 3527
As I see it, you have two problems in the code. The first one is that you are inflating the view during the onClick so obviously it is empty. The inflation should be done prior and only then you should register on the onClick event. Second, you're doing:
userInput.getText() instead of
userInput.getText().toString();
Upvotes: 1