Wait Reply Alertdialog

Asked

Viewed 224 times

0

I have an android app with a home screen, where there is a , and when I click on the she opens a , that displayed the following message "Want to Start Maintenance ?", and has two buttons "YES" and "NO", if the user cleaves into "YES", then he should open another screen and pass the parameters of "position" who are in the onItemClick, if the user clicks NO stay on the same screen, how can I do ?

@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
    AlertDialog.Builder msgBox = new AlertDialog.Builder(this);
    msgBox.setTitle("Atenção!");
    msgBox.setIcon(android.R.drawable.alert_dark_frame);
    msgBox.setMessage("Deseja iniciar Manutenção na ETE?");
    msgBox.setPositiveButton("SIM", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Cliente cliente = adpClientes.getItem(position);
            Intent intent = new Intent(MainActivity.this, init_manutencao.class);
            intent.putExtra("CLIENTE", cliente);
            startActivity(intent);
        }
    });
    msgBox.setNegativeButton("NÂO", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(MainActivity.this, "Saindo", Toast.LENGTH_SHORT).show();
        }
    });
    msgBox.create();
    msgBox.show();
}

  • When you speak "await the reply of Alertdialog" you refer to the click event of AlertDialog?

  • yes, it should wait for the user to click yes, if he clicks yes he passes to the next screen, if he clicks does not continue on the same screen.

  • but I also have some position parameters in my onItemClick, which should only be passed if the user click yes

  • The AlertDialog opens or just does nothing at the click of the button?

  • a Alertdialog opens, and asks if the user wants to proceed to next phase.

  • To avoid extensive discussion of the problem, edit your question and make it more specific by indicating possible error messages or unexpected code behavior

  • I changed the question according to your recommendation Ivan, please if you can help, I will be grateful.

  • I don’t understand your doubt. If you want something to be done when the "YES" button is clicked, the code that does that "something" should be placed where it is Toast.makeText(MainActivity.this, "Iniciando Manutenção", Toast.LENGTH_SHORT).show();

  • note that I am calling the alertDialog in my public void onItemClick just below I have the screen call, however I need to wait for the user to click YES

Show 4 more comments

1 answer

2


Come on, Leonardo!

If I understand your problem correctly, we can solve it this way

@Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            AlertDialog.Builder msgBox = new AlertDialog.Builder(this);
            msgBox.setTitle("Atenção!");
            msgBox.setIcon(android.R.drawable.alert_dark_frame);
            msgBox.setMessage("Deseja iniciar Manutenção na ETE?");
            msgBox.setPositiveButton("SIM", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {
                     Cliente cliente = adpClientes.getItem(position);
                     Intent intent = new Intent(ActivityAtual.this, ActivityQueRecebeOsDados.class);
                            intent.putExtra("CLIENTE", cliente);
                     startActivity(intent);
               }
            });
            msgBox.setNegativeButton("NÂO", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {
                  Toast.makeText(MainActivity.this, "Saindo", Toast.LENGTH_SHORT).show();
               }
            });
            msgBox.create();
            msgBox.show();
    }

I took the liberty of restructuring your code excluded the function Popup() and creating the AlertDialog directly inside the item click event.

As I pointed out at the beginning, if understood, you want to capture the client object at the click position and send this data to another Activity (screen, as I said). For such,

1 - You need to pass your dice Cliente, which is an object, via Intent

2 - Send the Intent calling for startActivity(Intent i) and not startAcivityForResult

3 - To retrieve the object in the other Activity, in onCreate, do `

    Cliente client;
    if(getIntent() != null) {
       client = getIntent().getParcelableExtra("CLIENTE");
    }

Attention: it is necessary that your object Cliente implement the interface Parcelable

  • It worked perfectly, I had tried this way but I got error because I was not passing Activityatual, and I was getting error because position was not accessible inside onClick. Thank you very much.

  • I’m glad I could help you. Good luck!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.