我想在用户点击AlertDialog中的“OK”时将变量传递给外部函数。 我正在尝试这个例子,但它不会识别变量(YUP)。Android:从AlertDialog传递一个变量onClick
public final void deleteBookmark(Cursor cur, int pos) {
//fetching info
((Cursor) cur).moveToPosition(pos);
String bookmark_id = ((Cursor) cur).getString(((Cursor) cur).getColumnIndex(Browser.BookmarkColumns._ID));
String bookmark_title = ((Cursor) cur).getString(((Cursor) cur).getColumnIndex(Browser.BookmarkColumns.TITLE));
//asking user to approve delete request
AlertDialog alertDialog = new AlertDialog.Builder(Dmarks.this).create();
alertDialog.setTitle("Delete" + " " + bookmark_title);
alertDialog.setIcon(R.drawable.icon);
alertDialog.setMessage("Are you sure you want to delete this Bookmark?");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
**String Yup = "yes";**
} });
alertDialog.setButton2("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Context context = getApplicationContext();
Toast.makeText(context, "canceled" , Toast.LENGTH_SHORT).show();
} });
alertDialog.show();
**if (Yup == "yes")** {
//deleting if user approved
getContentResolver().delete(Browser.BOOKMARKS_URI, "_id = " + bookmark_id, null);
//notifying user for deletion
Context context = getApplicationContext();
Toast.makeText(context, bookmark_title + " " + "deleted" , Toast.LENGTH_SHORT).show();
}
}
我知道代码有点搞砸了,但它只是为了理解。
感谢帮助!
定义变量**字符串烨=“是”; **了从功能“点击”,并在该函数分配价值为。例如在“// fetching info”区域中查看:http://pastie.org/1168788 – 2010-09-19 20:32:19
感谢您的回复。它仍然不会识别内部的“YUP”,只有外部的一个......这意味着什么都没有用户选择删除。:\ – 2010-09-19 20:52:23
顺便说一句,没有理由做:'((Cursor )cur).moveToPosition(pos);'...只是使用:'cur.moveToPosition(pos);'。 – Cristian 2010-09-19 20:56:58