У меня есть AlertDialog
, который по какой-то странной причине не может получить доступ к окончательному int deptID
. При передаче значения ConfirmRemoval
-function значение верное, но когда я вхожу в диалог onClick
, окончательный int не определен!
Я даже пытался изменить это на глобальную переменную, но все равно не повезло. Кто-нибудь знает, что происходит?
@Override
public void onCreate(Bundle icicle){
super.onCreate(icicle);
this.setContentView(R.layout.generic_list);
Bundle extras = getIntent().getExtras();
if (extras == null) {return;}
this.getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> adv, View v,
int pos, long id) {
Cursor cursor = (Cursor)adv.getItemAtPosition(pos);
int deptID = cursor.getInt(cursor.getColumnIndex("DeptID"));
ConfirmRemoval(deptID);
return true; //NOTE! If returning false, the itemClick event will fire
}
});
}
private void ConfirmRemoval(final int deptID){
AlertDialog.Builder bld = new AlertDialog.Builder(this);
bld.setCancelable(false);
bld.setTitle(R.string.deptRemove);
bld.setMessage(R.string.deptRemoveMsg);
bld.setPositiveButton("OK", new AlertDialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Dept.RemoveDept(deptID);
dialog.dismiss();
GetDepartments();
}
});
bld.setNegativeButton("Cancel", new AlertDialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = bld.create();
alert.show();
}
Спасибо, Руни
Вместо
Dept.RemoveDept(deptID)
int deptIDlocal = deptID; Dept.RemoveDept(deptIDlocal)
Хорошо, вы должны написать свой собственный OnClickListener - что-то вроде:
public static class processAlert implements DialogInterface.OnClickListener {
int deptID;
processAlert(int _id){
deptID = _id;
}
public void onClick(DialogInterface dialog, int which) {
Dept.RemoveDept(deptID);
dialog.dismiss();
GetDepartments();
}
}
и при создании диалога
bld.setPositiveButton("OK",new processAlert(deptID))
пытаться
final int deptID = cursor.getInt(cursor.getColumnIndex("DeptID"));