2014-09-16 3 views
0

내가 URL로 리다이렉트하기 위해 안드로이드에 AlertDialog를 사용하는 URL로, 사용자의 선택에 따라 이동해야 리디렉션, 여기에 코드입니다 :에 AlertDialog 리디렉션

final CharSequence[]stringArray = {"1" ,"2" , "3"}; 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setTitle("Press selected name"); 
    builder.setItems(stringArray, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int item) { 
      String st = "https://www.youtube.com/watch?v=x9QyKNQ0uVc"; 
      String st2 = "https://www.youtube.com/"; 
      if (stringArray.toString().equals("1")) { 
       Intent intent = new Intent(Intent.ACTION_VIEW, 
         Uri.parse(st)); 
        startActivity(intent); 
      } 
      else if (stringArray.toString().contains("2")) {     

        Intent intent = new Intent(Intent.ACTION_VIEW, 
          Uri.parse(st2)); 
         startActivity(intent);     
            } 
     } 
    }); 
    AlertDialog alert = builder.create(); 
    alert.show(); 

하지만 클릭하면 1 또는 2가 URL에 리디렉션이 없습니다. 코드에 어떤 문제가 있습니까?

+0

당신이 시도하고 확인? – GrIsHu

+0

내 대답을 확인하십시오. – GrIsHu

답변

0

구현할 수없는 배열 항목을 확인하고 있습니다. onclick 메서드에서만 얻을 수있는 선택한 항목의 ID를 확인해야합니다.

어레이 카운트가 "0"부터 시작되므로 선택한 항목을 "0-2"로 선택하면 0이 "1"로 선택됩니다. 코드 아래

체크 아웃 :

final CharSequence[] stringArray = { "1", "2", "3" }; 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setTitle("Press selected name"); 
    builder.setItems(stringArray, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int item) { 
      String st = "https://www.youtube.com/watch?v=x9QyKNQ0uVc"; 
      String st2 = "https://www.youtube.com/"; 
      if (item == 0) 
      // if (stringArray.toString().equals("1")) 
      { 
       Intent intent = new Intent(Intent.ACTION_VIEW, Uri 
         .parse(st)); 
       startActivity(intent); 
      } else if (item == 1) 
      // else if (stringArray.toString().contains("2")) 
      { 

       Intent intent = new Intent(Intent.ACTION_VIEW, Uri 
         .parse(st2)); 
       startActivity(intent); 
      } 
     } 
    }); 
    AlertDialog alert = builder.create(); 
    alert.show(); 
0

다음 코드를 사용하십시오 : - 디버깅

final CharSequence[] stringArray = { "1", "2", "3" }; 

AlertDialog.Builder builder = new AlertDialog.Builder(this); 

builder.setTitle("Press selected name"); 

builder.setItems(stringArray, new DialogInterface.OnClickListener() { 

public void onClick(DialogInterface dialog, int position) { 

String st = "https://www.youtube.com/watch?v=x9QyKNQ0uVc"; 

String st2 = "https://www.youtube.com/"; 

if (position == 0) { 

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(st)); 

startActivity(intent); 

} 

else if (position == 1) { 

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(st2)); 

startActivity(intent); 

} 

else if (position == 2) { 

// write some code 

} 

} 

}); 

AlertDialog alert = builder.create(); 

alert.show();