0

This function create my alert dialog, and add two listeners, one to display the dialog when the users click on the EditText, and one to dismiss and fill the EditText when an AlertDialog value is selected.

I'm trying to retrieve the selected value of a AlertDialog, in order to fill an EditText.

I know where I can retrieve this value, but unfortunately, I don't know how.

public void addSex () {
    final AlertDialog.Builder builder = new AlertDialog.Builder(SignupActivity.this);
    builder.setTitle("Votre sexe").setItems(R.array.sex_array,
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //Get dialog selected value
                }
            }
    );

    _sexText.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            AlertDialog dialog = builder.create();
            if (event.getAction() == MotionEvent.ACTION_DOWN)   dialog.show();
            return false;
        }
    });
}
Adrien Chapelet
  • 386
  • 4
  • 24

2 Answers2

1
public class ABC extends Activity {
    private String result;

    // other activity stuff 

    void showDialog(){
    AlertDialog.Builder b = new AlertDialog.Builder(this);
    b.setTitle("Please enter a password");
    final EditText input = new EditText(this);
    b.setView(input);
    b.setPositiveButton("OK", new DialogInterface.OnClickListener()
    {
        @Override
        public void onClick(DialogInterface dialog, int whichButton)
        {
           // SHOULD NOW WORK
           result = input.getText().toString();
        }
    });
    b.setNegativeButton("CANCEL", null);
    b.create().show();
    }
}

Reference : How can I get the results from an AlertDialog?

Community
  • 1
  • 1
KishuDroid
  • 5,411
  • 4
  • 30
  • 47
1

Simply use which value to know selected element.

    @Override
public void onClick(DialogInterface dialog, int which) {
            //Get dialog selected value
            String[] sexArray =  getResources().getStringArray(R.array.sex_array);
            _sexText.setText(sexArray[which]);    

}