Eclipse detect error on the implementation of setO

2019-08-29 02:58发布

I want to set my setOnShowListener() method to prevent dialog from closing, so I write something like this,

public Dialog update_pop_up()
{
    final AlertDialog.Builder builder = new AlertDialog.Builder(UgJadwal.this);
    LayoutInflater inflater = UgJadwal.this.getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.updatepopup, null);
    builder.setView(dialogView);
    builder.setIcon(R.drawable.update);
    builder.setTitle("Update schedule");

    builder.setPositiveButton(R.string.update, new DialogInterface.OnClickListener()
    {
        @Override
        public void onClick(DialogInterface dialog, int id)
        {

        }
    });

    builder.setNeutralButton(R.string.retrieve, new DialogInterface.OnClickListener()
    {
        @Override
        public void onClick(DialogInterface dialog, int id)
        {

        }
    });

    builder.setNegativeButton(R.string.cancel, null);
    return builder.create();

    builder.setOnShowListener(new DialogInterface.OnShowListener()
    {
        @Override
        public void onShow(DialogInterface dialog)
        {
            Button retrieve = builder.getButton(AlertDialog.BUTTON_NEUTRAL);
            retrieve.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    //do nothing;
                }
            });
        }
    });
}

At least there are two error prompted by eclipse, the first one is on the setOnShowListener that say "The method setOnShowListener(new DialogInterface.OnShowListener(){}) is undefined for the type AlertDialog.Builder" and the other one is on the builder.getButton(AlertDialog.BUTTON_NEUTRAL) that say "The method getButton(int) is undefined for the type AlertDialog.Builder". I just wonder why it raises errors becuase I have do it right I think. Does everyone have an idea to fix it, thanks.

[UPDATE!!] Thanks everybody for your help. This is my final methoD on setOnShowListener()

dialog.setOnShowListener(new DialogInterface.OnShowListener()
    {
        @Override
        public void onShow(DialogInterface dialog)
        {
            Button retrieve = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEUTRAL);
            retrieve.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    datasource.opentoread();
                    String[] hari = datasource.fetchDaily(ids.getText().toString());
                    datasource.close();
                    day.setText(String.valueOf(hari[0]));
                    subject.setText(String.valueOf(hari[1]));
                    time.setText(String.valueOf(hari[2]));
                    location.setText(String.valueOf(hari[3]));
                    lecture.setText(String.valueOf(hari[4]));
                    Toast.makeText(getApplicationContext(), "Retrieve success", Toast.LENGTH_LONG).show();
                }
            });
        }
    });
    return dialog;

3条回答
SAY GOODBYE
2楼-- · 2019-08-29 03:31

the first one is on the setOnShowListener that say "The method setOnShowListener(new DialogInterface.OnShowListener(){}) is undefined for the type AlertDialog.Builder"

There are two reasons why you'll get an error with this:

builder.setNegativeButton(R.string.cancel, null);
return builder.create();

builder.setOnShowListener(new DialogInterface.OnShowListener() ...
  1. You will never reach any code after calling return.
  2. setOnShowListener() is a method in AlertDialog, not AlertDialog.Builder.

that say "The method getButton(int) is undefined for the type AlertDialog.Builder"

This is the same problem, getButton() is a method in AlertDialog, not AlertDialog.Builder.


Try something like this:

...
builder.setNegativeButton(R.string.cancel, null);

final AlertDialog dlg = builder.create();
dlg.setOnShowListener(new DialogInterface.OnShowListener()
{
    @Override
    public void onShow(DialogInterface dialog)
    {
        Button retrieve = dlg.getButton(AlertDialog.BUTTON_NEUTRAL);
        // Changed this   ^^^
        retrieve.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //do nothing;
            }
        });
    }
});
return dlg;
查看更多
欢心
3楼-- · 2019-08-29 03:48

Or instead of creating an AlertDialog, you could just use a standard Dialog and then dismiss it when you want or listen to when the user closes the dialog. This would be a better alternative instead of using the default behavior of a button click closing the Dialog (AlertDialog).

查看更多
劫难
4楼-- · 2019-08-29 03:52

Looking at the AlertDialog.Builder documentation, setOnShowListener is not a valid method of AlertDialog.Builder. However, it is a valid method of AlertDialog, which is the return type for builder.create(). Similarly for getButton which is actually a method of AlertDialog. Therefore your code should be:

AlertDialog alertDialog = builder.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener()
{
    @Override
    public void onShow(DialogInterface dialog)
    {
        Button retrieve = alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL);
        retrieve.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //do nothing;
            }
        });
    }
});
查看更多
登录 后发表回答