Update a Switch from a Dialog

Asked

Viewed 122 times

0

I have a dialog like this:

public void Dialogo_Iluminacao() {


    final Dialog dialog = new Dialog(this);

    dialog.setContentView(R.layout.dialogo_iluminacao);

    dialog.setTitle("Iluminação");

    final Switch garagem = (Switch) dialog.findViewById(R.id.switchGaragem);
    final Switch jardim = (Switch) dialog.findViewById(R.id.switchJardim);
    final Switch sala = (Switch) dialog.findViewById(R.id.switchSala);


    garagem.setChecked(luzgaragem);
    jardim.setChecked(luzjardim);
    sala.setChecked(luzsala);
    holofote.setChecked(luzholofote);
    fundo.setChecked(luzfundo);
    lateral.setChecked(luzlateral);

    garagem.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        doCommand((garagem.isChecked() ? "acenda" : "apague") + " garagem", false);
                        garagem.setChecked(luzgaragem);
        }
    });

    jardim.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            doCommand((jardim.isChecked() ? "acenda" : "apague") + " jardim", false);
        }
    });

    sala.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            doCommand((sala.isChecked() ? "acenda" : "apague") + " sala", false);
        }
    });
 dialog.show();
}

when I click on the Switch garage, garden or living room it calls a function that if it works return a Boolean. Problem I can only change the status of the on/off switch after I get this return as I should do this?

I need to change the status of the Switch when I have a return on this function

public void OnStatusDaLuzRecebido(final Boolean b, final String setor) {

    new Thread() {
        public void run() {
            try {
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {


                        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
                        DateFormat horaformat = new SimpleDateFormat("HH:mm:ss");
                        Date date = new Date();
                        String dataagora = dateFormat.format(date);
                        switch (setor) {
                            case "garagem":
                                luzgaragem = b;
                                Toast.makeText(getApplicationContext(), (b ? " acesa " : "apagada"), Toast.LENGTH_LONG).show();
                                break;
                            case "jardim":
                                luzjardim = b;
                                Toast.makeText(getApplicationContext(), "Jardim " + (b ? "aceso" : "apagado"),
                                                Toast.LENGTH_LONG).show();
                                break;
                            case "sala":
                                luzsala = b;
                                Toast.makeText(getApplicationContext(), "Luz " + (b ? "acesa" : "apagada"), Toast.LENGTH_LONG).show();
                                break;
    });
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }.start();

}
  • The function to which it refers is doCommand?

  • This function is asynchronous?

No answers

Browser other questions tagged

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