My custom Dialog only appears after the whole run

0

I'm having a problem consuming a custom Dialog in an Android Activity.

I select the text that appears in the Dialog according to the previous answer and the accumulation of "yes" or "no" answers from the user, the problem is that I tried to put a "for" to be able to view these questions until the final result and it actually generates X dialogs with the same question instead of waiting for the answer to generate the next one.

Can anyone help me?

Follow the code:

    public void realizaPerguntas() {

    for (int i = 0; i < totalSintomas(); i++) {

        if (primeira == "") {
            primeira = "1";
            pergunta(v, pegaPrimeiraPergunta(v));
        } else {
            pergunta(v, pegaOutrasPerguntas(v));
            if (checaResultado().equalsIgnoreCase("FIM")) {
                Toast.makeText(contextoApp,
                        "Você pode ter: " + possiveldoenca,
                        Toast.LENGTH_LONG).show();
                return;
            }
        }

    }

    }
    public String pegaPrimeiraPergunta(View v) {

    b = new Banco(contextoApp, "Doencas.db", null, 1);
    int maisocorrencias = 0;
    String sintoma = "";

    Cursor c = b
            .getConnection()
            .rawQuery(
                    "SELECT CODSIN, COUNT(CODSIN) AS SINTOMAS FROM SINTOMA_DOENCA GROUP BY CODSIN ORDER BY SINTOMAS DESC",
                    null);
    if (c.moveToFirst()) {
        do {

            if (c.getInt(1) >= maisocorrencias) {
                maisocorrencias = c.getInt(1);
                sintoma = c.getString(0);
            }
        } while (c.moveToNext());
    }

    return sintoma;

}
    public String pegaOutrasPerguntas(View v) {

    b = new Banco(contextoApp, "Doencas.db", null, 1);
    int maisocorrencias = 0;
    String sintoma = "";

    String param = dnotin;
    String param2 = in;

    if (param.length() == 0) {
        if (param2.length() == 0) {
            param = "";
            param2 = "";

        } else {
            param = dnotin.substring(0, dnotin.length() - 1);
            param2 = in.substring(0, in.length() - 1);
        }
    }
    Cursor c = b
            .getConnection()
            .rawQuery(
                    "SELECT CODSIN, COUNT(CODSIN) AS SINTOMAS FROM SINTOMA_DOENCA WHERE CODDOE NOT IN (?) AND CODSIN NOT IN (?) GROUP BY CODSIN ORDER BY SINTOMAS DESC",
                    new String[] { param, param2 });
    if (c.moveToFirst()) {
        do {

            if (c.getInt(1) >= maisocorrencias) {
                maisocorrencias = c.getInt(1);
                sintoma = c.getString(0);
            }
        } while (c.moveToNext());
    }

    return sintoma;

}
    public String checaResultado() {

    b = new Banco(contextoApp, "Doencas.db", null, 1);

    int qtd = conta_ocorrencias(',', in);

    if (in.length() == 0) {

        b.close();
        return "";

    } else {
        Cursor c = b
                .getConnection()
                .rawQuery(
                        "SELECT DISTINCT CODDOE FROM SINTOMA_DOENCA WHERE CODSIN IN (?)",
                        new String[] { in.substring(0, in.length() - 1) });

        if (c.getCount() == qtd) {

            if (c.moveToFirst()) {
                do {
                    possiveldoenca = c.getString(0);
                } while (c.moveToNext());
            }

            b.close();

            return "FIM";
        } else {
            b.close();
            return "";
        }
    }
}
    public void pergunta(View v, final String sintoma) {

    final Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.pergunta);
    dialog.setTitle("Login");
    dialog.setCancelable(false);
    Button sim = (Button) dialog.findViewById(R.id.btsim);
    Button nao = (Button) dialog.findViewById(R.id.btnao);
    tvpergunta = (TextView) dialog.findViewById(R.id.tvpergunta);

    String perg = "";
    b = new Banco(contextoApp, "Doencas.db", null, 1);

    Cursor c = b.getConnection().query("SINTOMA",
            new String[] { "CODSIN", "DESSIN" }, "CODSIN = ?",
            new String[] { sintoma }, null, null, null, null);
    if (c.moveToFirst()) {
        do {
            perg = c.getString(1);

        } while (c.moveToNext());
    }

    tvpergunta.setText("Você tem " + perg + "?");

    sim.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            in += sintoma + ",";
            dialog.dismiss();
        }
    });

    nao.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            snotin += sintoma + ",";
            notInDoencas();

            dialog.dismiss();
        }
    });

    dialog.show();
}
    
asked by anonymous 13.05.2015 / 13:49

1 answer

0

Dialogs run asynchronously: the show() method returns immediately upon being called, continuing the program execution on the next line. That is why during execution of for several Dialogs are opened.

It is not possible / easy to analyze your code in detail however I suggest two options:

1 - Replace Dialog with an Activity / Fragment.
2 - Manage the need for new questions within the sim.setOnClickListener() method of Dialog.

    
13.05.2015 / 14:58