AlertDialog에서 '데이터베이스에서 삭제'버튼을 클릭하도록 확인하려고합니다. 꽤 오랜 시간 동안 노력 중이며 작업 모델을 얻을 수 없습니다. 현재 코드는 다음과 같습니다.Android에서 AlertDialog를 사용하여 삭제 확인
클래스의 상단 부울
private static boolean dialogResult;
방법은 다이얼로그
setDialogResult (BOOL)는 단지 상기 클래스 변수를 설정private void showErrorDialog(String title, String message){
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle(title)
.setMessage(message)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Activity.setDialogResult(true);
dialog.dismiss();
}
})
.setNegativeButton(R.string.cancel,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Activity.setDialogResult(false);
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
를 표시한다. '삭제'버튼이 코드를 누르면
은 실행 :
setDialogResult(false);
//Find which list is to be deleted
RadioButton selectedBtn=(RadioButton)findViewById(group.getCheckedRadioButtonId());
String currentSelectedListName=selectedBtn.getText().toString();
if(!currentSelectedListName.equals("All")){//check if the list is the default
showErrorDialog("","Delete: "+currentSelectedListName+" ?");
if(dialogResult){
mDbHelper.deleteFGList(currentSelectedListName);
this.populateRadioGroupFromDb();
}
} else{//if it is the default (That is, the name of the list is "All") tell user of error
showErrorDialog("Delete Error","The default list cannot be deleted");
}
//}
이 도움을 주셔서 감사합니다!
편집 : 죄송합니다. 오류가 있음을 잊어 버렸습니다. 경고 대화 상자에서 확인을 누르면 데이터베이스에서 항목이 삭제되지 않습니다. 어떻게 든 그것은 부울 검사를 건너 뜁니다. 해당 코드 단편의 시작 부분에서 부울 값을 false로 설정할 수 있습니다. 삭제되면 대화 상자의 '삭제'버튼, 확인 버튼을 누르면 아무 일도 일어나지 않습니다. '삭제'버튼을 다시 누르면이 새로운 선택 항목이 대화 상자 검사없이 자동으로 삭제됩니다. 기본적으로, 그 onClick 메서드는 엉망이고, 나는 그것을 할 수있는 더 좋은 방법이 있기를 바랐다. 인터페이스 사용에 대해 읽은 적이 있습니까?
은 그래서 사람들이
if(!currentSelectedListName.equals("All")){//check if the list is the default
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("")
.setMessage("Delete: "+currentSelectedListName+" ?")
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mDbHelper.deleteFGList(currentSelectedListName);
Activity.populateRadioGroupFromDb();
}
})
.setNegativeButton(R.string.cancel,null);
AlertDialog alert = builder.create();
alert.show();
} else{//if it is the default (That is, the name of the list is "All") tell user of error
showErrorDialog("Delete Error","The default list cannot be deleted");
}
문제가 일식
mDbHelper.deleteFGList(currentSelectedListName);
Activity.populateRadioGroupFromDb();
이 내부 클래스 내부가 아닌 최종 변수 currentSelectedListName를 참조 1.Cannot에 컴파일 오류를 준다이다이 방법을 제시 한 것 다른 메서드에서 정의 됨 2. MyFGCardsActivity 형식의 non-static 메서드 populateRadioGroupFromDb()에 대한 정적 참조를 만들지 못함
이것이 옹호 된 접근입니까, 아니면 오해입니까?
무엇이 오류입니까? –
어떤 문제가 있습니까? 대화 상자가 표시됩니까? – Chandrashekhar