我有一个AlertDialog
,由于一些奇怪的原因无法访问最终的int deptID
。
将值传递给ConfirmRemoval
- 函数时,值是正确的,但是当我进入对话框的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();
}
谢谢, Runey
答案 0 :(得分:1)
好的,你必须编写自己的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))
答案 1 :(得分:0)
试
final int deptID = cursor.getInt(cursor.getColumnIndex("DeptID"));
答案 2 :(得分:0)
而不是
Dept.RemoveDept(deptID)
做
int deptIDlocal = deptID; Dept.RemoveDept(deptIDlocal)