Android get text EditText to AlertDialog

3232 views java
6

I have to take the text from an AlertDialog.

For the AlertDialog I use a specific layout: custom_alertdialog

Instead for all the project use: another layout

I tried like this:

View layout = getLayoutInflater().Inflate(R.layout.custom_alertdialog,null);

Or so:

View layout = View.inflate(SimpleVideoStream.this, R.layout.custom_alertdialog, null);

But the text is not taken in either case. How can I do?

AlertDialog.Builder builder = new AlertDialog.Builder(SimpleVideoStream.this);
        builder.setTitle("Add Subtitle");
        builder.setCancelable(true);

        builder.setPositiveButton(
                "Ok",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        View inflatedView = getLayoutInflater().inflate(R.layout.custom_alertdialog, null);
                        final View layout = View.inflate(SimpleVideoStream.this, R.layout.custom_alertdialog, null);

                        EditText url_ele = (EditText) layout.findViewById(R.id.url);
                        String sub = url_ele.getText().toString();
                        Log.v("Ok:","/"+sub);
                        if (!sub.equals("")) {
                            showSub = !showSub;
                            Format textFormat = Format.createTextSampleFormat(null, MimeTypes.APPLICATION_SUBRIP,
                                    null, Format.NO_VALUE, Format.NO_VALUE, "en", null, Format.OFFSET_SAMPLE_RELATIVE);
                            MediaSource textMediaSource = new SingleSampleMediaSource.Factory(dataSourceFactory)
                                    .createMediaSource(Uri.parse(sub), textFormat, C.TIME_UNSET);

                            videoSource = new MergingMediaSource(videoSource, textMediaSource);
                            player.prepare(videoSource, false, false);
                        }
                        dialog.cancel();
                    }
                });

        builder.setNegativeButton(
                "Close",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });

        AlertDialog alert = builder.create();
        LayoutInflater inflater = getLayoutInflater();
        View dialoglayout = inflater.inflate(R.layout.custom_alertdialog, null);
        alert.setView(dialoglayout);
        alert.show();

answered question

1 Answer

6

When you inflate a View, it doesn't share an instance with any other times you've inflated it. So what you passed to setView() is completely different from what you're calling findViewById() on. You need to create only one instance and then reference that.

For example:

View view1 = inflater.inflate(R.layout.some_layout, null);
View view2 = inflater.inflate(R.layout.some_layout, null);

EditText text1 = view1.findViewById(R.id.some_edittext);
EditText text2 = view2.findViewById(R.id.some_edittext);

text1.setText("Hello");

String text2text = text2.getText().toString(); //this will be null, because text1 and text2 aren't the same instance, because view1 and view2 aren't the same instance.

Move

View dialoglayout = inflater.inflate(R.layout.custom_alertdialog, null);

above where you set the positive button, and make it final:

final View dialoglayout = inflater.inflate(R.layout.custom_alertdialog, null);
builder.setPositiveButton(...

Remove both inflatedView and layout from inside your listener, and use dialogLayout:

EditText url_ele = (EditText) dialogLayout.findViewById(R.id.url);

posted this

Have an answer?

JD

Please login first before posting an answer.