My custom Dialog only appears after the to run all

Asked

Viewed 89 times

0

I’m having a problem to consume 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 answers "yes" or "no" of the user, the problem is that I tried to put a "for" to be able to display these questions until you get 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 someone help me?

Follows 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();
}
  • You speak in a Dialog but I see no call to Dialog in your code.

  • Opa, my fault, missed the "question" function that generates the dialogs. I will edit

1 answer

0

Dialogs are executed asynchronously: the method show() returned immediately after being called, continuing the execution of the program on the next line.
That’s why during the execution of for several dialogs are opened.

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

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

Browser other questions tagged

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