0

I try to remember the checkbox within a dialogue but in comparison remember == false the variable remember still has not changed its value. In the loop is iterated as many times as the size of lelements_tmp before call showDialogSameFile() so neither can I use the variable remember within the loop. How can I do it? Thanks in advance.

private boolean remember = false;

// in the program
// add files to List lelements_tmp
while (i < lelements_tmp.size()) {
    File fto = new File(lelements_tmp.get(i).getFile());
    String to = null;
    try {
        to = fto.getCanonicalPath();
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (fto.exists()) {
        showDialogSameFile(fto, to, i);
        i++;
        continue;
    }

    thread(i);
    i++;
}

private void showDialogSameFile(File f, String to, final int i) {
        TextView title = null;
        if (f.isDirectory())
            title = this.getTitle("The directory " + to + " already exists. Do you want to replace the contents?");
        else if (f.isFile())
            title = this.getTitle("The file " + to + " already exists. Do you want to replace it?");

        String[] item = {"Apply to all"};
        AlertDialog ad = new AlertDialog.Builder(context)
                .setCustomTitle(title)
                .setMultiChoiceItems(item, null,
                        new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int indexSelected, boolean isChecked) {
                                if (isChecked)
                                    remember = true;
                            }
                        })
                .setPositiveButton("Aceptar",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                thread(i);
                            }
                        })
                .setNegativeButton("Cancelar",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                .create();
    if (remember == false)
            ad.show();
}
Javi
  • 188
  • 1
  • 9
  • 1
    wow, you mixed order of execution there, you are never going too see remember being true this way. I assume you are trying to use this after dialog dismisses? Add listener on dialog close, and then ask whether remember is false or not. – Marko Lazić Aug 09 '15 at 21:08

1 Answers1

0

I've had to refactor the code a bit, but basically with this work. It's necessary to call to the method from non-UI thread. Based on this response https://stackoverflow.com/a/11369224/5089855

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;

import java.util.concurrent.Semaphore;

public class Proof {

    private Semaphore mutex = new Semaphore(0, true);
    private boolean remember = false;
    private Activity context;

    public Proof(Activity context) {
        this.context = context;
    }

    private boolean showDialogMismoArchivo() {
        context.runOnUiThread(new Runnable() {
            public void run() {
                String[] item = {"Apply all"};
                AlertDialog ad = new AlertDialog.Builder(context)
                        .setTitle("¿Reemplazar?")
                        .setMultiChoiceItems(item, null,
                                new DialogInterface.OnMultiChoiceClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int indexSelected, boolean isChecked) {
                                        if (isChecked)
                                            remember = true;
                                        else
                                            remember = false;
                                    }
                                })
                        .setPositiveButton("Ok",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        mutex.release();
                                    }
                                })
                        .setNegativeButton("Cancel",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        mutex.release();
                                    }
                                })
                        .create();
                ad.show();
            }
        });

        try {
            mutex.acquire();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return remember;
    }
}
Community
  • 1
  • 1
Javi
  • 188
  • 1
  • 9