2

I've set up an alert dialog with multiple edit texts but I'm not sure how to store the values being entered in the alert dialog.

Usually I could just do something along the lines of this:

final EditText input = new EditText(this);
alert.setView(input);
Editable value = input.getText();

But my MessageDialog is a separate class being called from SearchResult.java like this, so I don't know how to access instances of the edit texts in the MyMessageDialog.java:

MyMessageDialog.displayMessage(SearchResult.this, "Sample Info", "Required");

Does anyone know how the edit text values can be retrieved in this implementation?

This is the MyMessageDialog class and below that the layout for the alert dialog:

public class MyMessageDialog  {

    @SuppressLint("NewApi") 
    public static AlertDialog displayMessage(Context context, String title, String message){ 
    AlertDialog.Builder builder = new AlertDialog.Builder(context); 
    LayoutInflater inflater = LayoutInflater.from(context);
    builder.setTitle(title); 
    builder.setMessage(message); 
    builder.setView(inflater.inflate(R.layout.custom_view, null));
    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 
    dialog.cancel(); 
    } 
    }); 
    builder.show(); 
    return builder.create(); 
    } 

}

Alert Dialog Layout, custom_view:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

     <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="ship name"
        android:id="@+id/shipNameEditText" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="analyst name"
        android:id="@+id/scientistEditText2" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="email address"
        android:id="@+id/emailEditText3"
        android:layout_gravity="center_horizontal" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="sample volume"
        android:id="@+id/volumeEditText4" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="sample colour"
        android:id="@+id/colourEditText4" />


</LinearLayout>
Brian Var
  • 6,029
  • 25
  • 114
  • 212

1 Answers1

5

Add an interface to your MyMessageDialog class to pass the values back:

    public interface MyMessageDialogListener {
        public void onClosed(String ship, String scientist, String email, String volume, String color);
    }

Store the dialog layout when you create it and extract the EditText values and pass them back via the listener inside the OK button onClick:

public class MyMessageDialog  {

    public interface MyMessageDialogListener {
        public void onClosed(String ship, String scientist, String email, String volume,     String color);
    }

@SuppressLint("NewApi") 
public static AlertDialog displayMessage(Context context, String title, String message, final MyMessageDialogListener listener){ 
    AlertDialog.Builder builder = new AlertDialog.Builder(context); 
    LayoutInflater inflater = LayoutInflater.from(context);
    builder.setTitle(title); 
    builder.setMessage(message); 
    final View layoutView = inflater.inflate(R.layout.custom_view, null);
    builder.setView(layoutView);
    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 

    // get the edit text values here and pass them back via the listener
    if(listener != null)
    {
        EditText text1 = (EditText)layoutView.findViewById(R.id.shipNameEditText);
        EditText text2 = (EditText)layoutView.findViewById(R.id.scientistEditText2);
        EditText text3 = (EditText)layoutView.findViewById(R.id.emailEditText3);
        EditText text4 = (EditText)layoutView.findViewById(R.id.volumeEditText4);
        EditText text5 = (EditText)layoutView.findViewById(R.id.colourEditText4);

        listener.onClosed(text1.getText().toString(),
            text2.getText().toString(),
            text3.getText().toString(),
            text4.getText().toString(),
            text5.getText().toString());
        }

        dialog.cancel(); 
    } 
    }); 
    builder.show(); 
    return builder.create(); 
    } 

}

Create an instance of the listener when you call the dialog and use it to receive the strings:

MyMessageDialog.displayMessage(SearchResult.this, "Sample Info", "Required",
    new MyMessageDialog.MyMessageDialogListener() {
        public void onClosed(String ship, String scientist, String email, String volume, String color)
        {
            // store / use the values here
        }
    });
samgak
  • 23,944
  • 4
  • 60
  • 82
  • I tried to implement the above solution but get the following error: `Cannot refer to a non-final variable listener inside an inner class defined in a different method` at line "if(listener != null)" and a second error on edit texts `layoutView cannot be resolved` – Brian Var Jan 04 '15 at 02:38
  • also, note that builder.setView(inflater.inflate(R.layout.custom_view, null)); has changed to final View layoutView = inflater.inflate(R.layout.custom_view, null); builder.setView(layoutView); (unchanged in the hastebin code) – samgak Jan 04 '15 at 04:51