guydroid123
guydroid123

Reputation: 9

for loop array list alert dialog

I have a Button click which picks all the checked CheckBoxes in a ListView and shows all the checked boxes in an AlertDialog.

I iterate all elements with in a for-loop and I have a positiveButton and a negativeButton - and when I press the negativeButton I want to cancel all the dialogs, and go back to the app.
But instead, when I press the negativeButton, it iterates back one by one.

btnDialog = (Button) findViewById(R.id.btnDialog);
btnDialog.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        StringBuffer responseText = new StringBuffer();
        //responseText.append("הפריטים שנבחרו הם: \n");
        Models models = new Models();
        //List<Models> modelList = new ArrayList<>();
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
        alertDialogBuilder.setTitle("Diet Product");
        responseText.append("הפריטים שנבחרו הם: \n");
        Log.d(responseText.toString(), msg);

        for (int i = 0; i < (modelList.size()); i++) {
            models = modelList.get(i);
            if (models.isSelected()) {
                alertDialogBuilder
                        .setMessage(responseText.append(models.getName() + " : " + models.getProtein() + "\n"))
                        .setCancelable(false)
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                // if this button is clicked, close current activity
                                MainActivity.this.finish();
                            }
                        })
                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {

                                dialog.dismiss();
                            }
                        });
                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();

            }
        }

    }
});

Upvotes: 0

Views: 869

Answers (1)

guydroid123
guydroid123

Reputation: 9

I believe that I came to solve this issue

        btnDialog = (Button) findViewById(R.id.btnDialog);
        btnDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                StringBuffer responseText = new StringBuffer();
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
                alertDialogBuilder.setTitle("Diet Product");
                responseText.append("הפריטים שנבחרו הם: \n");

                for(int i=0;i<modelList.size();i++){
                    Models models = modelList.get(i);
                    if(models.isSelected()){
                        responseText.append("\n" + models.getName()+ " : "+models.getProtein() +"\n");
                    }
                }

                alertDialogBuilder
                        .setMessage(responseText.append("\n"+"לחישוב ערכים לחץ המשך " +"\n"+"או חזור לעידכון פרטים"+ "\n"))
                        .setCancelable(false)
                        .setPositiveButton("המשך", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                // if this button is clicked, close current activity
                                //MainActivity.this.finish();
                                Intent intent = new Intent(getApplicationContext(),ItemActivity.class);
                                intent.putExtra(models.getName(),"name");
                                startActivity(intent);
                            }
                        })
                        .setNegativeButton("חזור", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {

                                dialog.cancel();
                            }
                        });
                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();

Upvotes: 1

Related Questions