views:

144

answers:

1

Hi!

I want to pass a variable to an outer function when user clicks on "OK" in AlertDialog. I'm trying this for example but it won't recognize the Variable (Yup).

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();
        }
    }

I know the code is a bit messed up but it's only for the sake of understanding.

Appreciate the help!

+1  A: 

Yup is not being recognized because you create the string in the onClick method, and it gets recycled when onClick is done.

I recommend just getting rid of Yup, because even if you fix this, you'll have problems. The dialog will pop up, but by the time the user selects, the application will already have gone through the if statement, so Yup never has a chance to equal "Yes". In other words, the dialog box doesn't pause your code and wait for the user input before going through "if (Yup == "yes"). Also, the if statement should look like this: if (Yup.equals("yes")), otherwise, it will return false everytime.

I would make your code look like this:

public final void deleteBookmark(Cursor cur, int pos) {

    //fetching info
    ((Cursor) cur).moveToPosition(pos);
    final String bookmark_id = ((Cursor) cur).getString(((Cursor) cur).getColumnIndex(Browser.BookmarkColumns._ID));
    final 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) {
             //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();
          } });
    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();
    }
}
magicman
That will be great but "bookmark_id" variable isn't recognized inside the onClick and that was the problem in the first place :\ Any idea how can I pass it inside? when I add it to the onClick method I get an error it's supposed to be an interface func.
liorry
cast bookmark_id as final: final String bookmark_id = ((Cursor) cur).getString(((Cursor) cur).getColumnIndex(Browser.BookmarkColumns._ID));
magicman
Perfect! :) Thanks a lot!
liorry