0

I sought numerous answers/similar questions and non solved my problem. I have a custom dialog implemented in a class that extends "DialogFragment". When I try to get text from any of the layout components I get the initial default text that I sat.

Code snippet :

public class input1_frag extends DialogFragment 
{
    View v;
    LayoutInflater inflater;
    EditText email_field,passwd_field;
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) 
    {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        inflater = getActivity().getLayoutInflater();
        builder.setTitle("Identity verification");
        // Inflate and set the layout for the dialog
        builder.setView(inflater.inflate(R.layout.input_dialog, null));
        builder.setPositiveButton("Modify account", new DialogInterface.OnClickListener() 
        {
                   public void onClick(DialogInterface dialog, int id) 
                   {
                     v = inflater.inflate(R.layout.input_dialog, null);
                     email_field   = (EditText) v.findViewById(R.id.input_dialog_email0);
                     passwd_field   = (EditText) v.findViewById(R.id.input_dialog_passwd0);

                    String fetched_email = email_field.getText().toString();

                    String fetched_passwd = passwd_field.getText().toString();
});}}

input.dialog.xml

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" >

    <TableRow
        android:id="@+id/tableRow4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Enter your old data"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </TableRow>

    <TableRow android:id="@+id/tableRow_space_1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">

         <View android:layout_width="fill_parent" 
             android:layout_height="20dp">
        </View>

    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <EditText
            android:id="@+id/input_dialog_email0"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:text="E-mail" />

    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <EditText
            android:id="@+id/input_dialog_passwd0"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:text="xxx" >

            <requestFocus />
        </EditText>

    </TableRow>

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </TableRow>

</TableLayout>

I made the view & the inflater global to avoid making them final. Yet it still returns the initial text from the textfield.

1 Answers1

0

Remove following line from your onClick method and use it in builder.setView(). Because of this line view is being reset to its original form and changes made to edit text is lost.

v = inflater.inflate(R.layout.input_dialog, null);

Like this

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    inflater = getActivity().getLayoutInflater();
    builder.setTitle("Identity verification");
    // Inflate and set the layout for the dialog
    v = inflater.inflate(R.layout.input_dialog, null);
    builder.setView(v);
    builder.setPositiveButton("Modify account", new DialogInterface.OnClickListener() 
    {
               public void onClick(DialogInterface dialog, int id) 
               {

                 email_field   = 
                 ...
                 ...
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
  • Thank you, that fixed the problem. But what is the cause of the problem ? – AtlasS2626 Dec 23 '14 at 21:47
  • The line I asked you to remove is used to create a view from xml in its original form. On click of "Modify account" button you were again creating the view. Because of this old view on which you entered the value was reset. – Rohit5k2 Dec 23 '14 at 21:58
  • I see, so builder.setView(v) is actually making view "v" linked to the builder all the time, which will allow it to capture any change to the xml layout elements. I understand that my original code was passing the default instance of the view via builder.setView(inflater.inflate(R.layout.input_dialog, null)); though it's not referenced through a variable, thus the updated version of the xml won't be accessed. – AtlasS2626 Dec 24 '14 at 09:56