2

ユーザーがAlertDialogで[OK]をクリックしたときに、変数を外部関数に渡したいのですが。たとえば、これを試していますが、変数を認識しません(うん)。

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

コードが少し混乱していることは知っていますが、それは理解のためだけのものです。

助けに感謝します!

4

1 に答える 1

6

onClickメソッドで文字列を作成したため、Yupは認識されません。文字列は、onClickが実行されるとリサイクルされます。

これを修正しても問題が発生するので、Yupを取り除くことをお勧めします。ダイアログがポップアップ表示されますが、ユーザーが選択するまでに、アプリケーションはすでにifステートメントを実行しているため、Yupが「Yes」に等しくなる可能性はありません。つまり、ダイアログボックスはコードを一時停止せず、ユーザー入力を待ってから「if(Yup == "yes"」を実行します。また、ifステートメントは次のようになります。if (Yup.equals("yes"))それ以外の場合はfalseを返します。毎回。

私はあなたのコードをこのように見せます:

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();
    }
}
于 2010-09-19T20:39:56.110 に答える