There is a dialog where the user should insert a link.

data is entered into EditText

dialogue code:

private void showAddVideoDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); builder.setTitle("Добавить видео"); View viewInflated = LayoutInflater.from(getContext()).inflate(R.layout.view_layout_add_video, (ViewGroup) getView(), false); final EditText input = (EditText) viewInflated.findViewById(R.id.edt_videoUrl); builder.setView(viewInflated); builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); Toast.makeText(getContext(), "ОК", Toast.LENGTH_SHORT).show(); String string = input.getText().toString(); onUserInput(string); getYouTubeId("https://"); } }); builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); Toast.makeText(getContext(), "Отмена", Toast.LENGTH_SHORT).show(); } }); builder.show(); } 

Then we call the onUserInput method, in which we start working with the string:

 private void onUserInput(String input){ String placeholder = "http://img.youtube.com/vi/%s/0.jpg"; String url = input.format(placeholder, id); } 

The problem is that when you try to pass a variable to the getYouTubeId method, the url we declared in onUserInput is not used (not passed)

 private String getYouTubeId (String url) { String pattern = "(?<=youtu.be/|watch\\?v=|/videos/|embed\\/)[^#\\&\\?]*"; Pattern compiledPattern = Pattern.compile(pattern); Matcher matcher = compiledPattern.matcher(url); if(matcher.find()){ return matcher.group(); } else { return "error"; } } 

Please tell me how to correct the method.

  • depends on how you show the dialogue. - Vladyslav Matviienko
  • @metalurgus updated the question - Inkognito
  • How this dialogue is caused and where it is necessary to transfer value from EditText? - pavlofff
  • The @pavlofff dialog is invoked through the interface public void clickVideo (int pos, View v) {showAddVideoDialog (); } and the value for example in some method for example. - Inkognito

1 answer 1

Declare an Activity (or Fragment , depending on where you call the showAddVideoDialog method) method:

 void onUserInput(String input) { //тут делайте со строкой что угодно, сохраните в переменную, например } 

And change your code like this:

 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); Toast.makeText(getContext(), "ОК", Toast.LENGTH_SHORT).show(); // добавить следующее String string = input.getText().toString(); onUserInput(string); } }); 
  • Thanks, but the question was to find out HOW you can save the variable in the string in your onUserInput method - Inkognito
  • @Inkognito, declare a variable with the name for example peremennaya , and write peremennaya = input . I thought it was obvious - Vladyslav Matviienko
  • 2
    @Inkognito brackets must be placed after getText :) - input.getText (). ToString (); - pavlofff
  • one
    after getText put (), should earn) - Morozov
  • one
    @Inkognito Declare your peremennaya variable peremennaya a class field and it will be available everywhere within the fragment. - pavlofff