2011-08-04 3 views
2

AlertDialog을 생성하는 AlertDialogUtils 클래스가있어서 오류가 발생했을 때 어떤 액티비티에서도 호출 할 수 있습니다. 문제는 createDialog() 메서드 내에서 finish()onClickListener으로 "dismiss"버튼으로 호출 할 수 없다는 것입니다.사용자 정의 대화 상자에서 활동을 닫는 방법 클래스?

어떤 생각이 가능할까요? AlertDialogUtils 클래스

코드 :

public class AlertDialogUtils extends Dialog { 

    private Context mContext; 

    public AlertDialogUtils(Context context) { 
     super(context); 
     mContext = context; 
    } 

    public void CreateAlertDialog(String errorMessage) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(mContext); 
     builder.setMessage(errorMessage) 
      .setCancelable(true) 
      .setNeutralButton("Dismiss", new AlertDialog.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        mContext.finish(); 
        //error here. Intend to close the activtiy that created this dialog and has the error 
       } 
      }); 
     AlertDialog alert = builder.create(); 
     alert.setOwnerActivity((Activity)mContext); 
     // The dialog utils is outside an activity. Need to set owner 
     alert.show(); 
    } 
} 
+0

mContext.finish() : 또한, 내가 exitActivity()를 호출하는 대신 닫고 싶지 않았다 활동을 위해, 나는 dialog.cancel()라고? –

+0

아마도 이것이 가장 직접적인 방법은 아니지만, 액티비티를 매개 변수로 'CreateAlertDialog (String)'에 전달하지 않으시겠습니까? – Brian

답변

2

당신이 이런 식으로하시기 바랍니다 시도시겠습니까? :

interface ICloseActivity { 
void close(); 
} 

class MyActivityToClose extends Activity implements ICloseActivity { 

public void close() { 
finish(); 
} 

} 

// ------- 

public class AlertDialogUtils extends Dialog { 

    private Context mContext; 

    private ICloseActivity mICloseActivity; 

    public AlertDialogUtils(Context context, ICloseActivity aActivity) { 
     super(context); 
     mContext = context; 
     mICloseActivity = aActivity; 
    } 

    public void CreateAlertDialog(String errorMessage) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(mContext); 
     builder.setMessage(errorMessage) 
      .setCancelable(true) 
      .setNeutralButton("Dismiss", new AlertDialog.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
       // mContext.finish(); 
       //error here. Intend to close the activtiy that created this dialog and has the error 

       //TRY THIS please: 
       mICloseActivity.close(); 
       } 
      }); 
    AlertDialog alert = builder.create(); 
    alert.setOwnerActivity((Activity)mContext); 
      // The dialog utils is outside an activity. Need to set owner 
    alert.show(); 
} 
2

생성자 대신에 Context를 전달, Activity 통과. Context을 상속하므로 어디서나 사용할 수 있습니다. Context; 동시에 필요할 때 finish()으로 전화 할 수도 있습니다.

2

제안 해 주셔서 감사합니다. ICloseActivity, 불행히도, 나를 위해 작동하지 않았다. 나는 활동을 마무리하고 다음과 같은 방식으로 대화 상자를 기각의 문제 해결 : 나는 다음과 같이 CreateAlertDialog() 방법을 변경하고 도우미 함수 exitActivity() 생성 :이 유틸리티를 호출, 단순히 해고 설정된 동안

public void CreateAlertDialog(String title, String errorMessage, int alertType){ 
    AlertDialog.Builder builder = new AlertDialog.Builder(mContext); 
      builder.setTitle(title) 
     .setIcon(R.drawable.alert_icon) 
     .setMessage(errorMessage) 
     .setCancelable(false) 
     .setPositiveButton("Dismiss", new AlertDialog.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          dismiss = true; 
          exitActivity(); 

         } 
        }); 
     AlertDialog alert = builder.create(); 
     alert.setOwnerActivity((Activity)mContext); 
      alert.show(); 
       } 
      public void exitActivity() { 
      this.getOwnerActivity().finish(); 
      } 

을 청취자가 그 활동을 끝내면 해산됩니다.

   dialog = new AlertDialogUtils(MyActivity.this); 
    dialog.setOwnerActivity(MyActivity.this); 
    dialog.CreateAlertDialog(getString(R.string.no_data),  
         getString(R.string.empty_table_message), R.id.list_view_alertType); 
    dialog.setOnDismissListener(new DialogInterface.OnDismissListener(){ 
     @Override 
     public void onDismiss(DialogInterface arg0) { 
      finish(); 
     } 
    }); 

이렇게하면 필요한 경우 현재 활동을 종료 할 수 있습니다.

   .setPositiveButton("Dismiss", new AlertDialog.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       cancel(); 

      } 
     }); 
관련 문제