Progress bar does not appear

Asked

Viewed 67 times

0

I entered everything in the code, but there is no popup bar, I can’t find the error, can anyone help me? Follow the code:

package com.example.fantin.cardapio;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Vector;

public class Cardapio extends AppCompatActivity {

    TextView txtCardapio;
    RadioGroup radioGroup;
    RadioButton rbCachorroQuente;
    RadioButton rbBauruSimples;
    RadioButton rbBauruOvo;
    TextView txt350;
    TextView txt400;
    TextView txt450;
    Spinner spinner;
    ImageView imageView;
    TextView txtNomeCliente;
    EditText edtNomeCliente;
    TextView txtQuantidade;
    EditText edtQuantidade;
    Button btnInclirConta;
    ListView lvLista;
    TextView txtTotal;
    EditText edtTotal;
    CheckBox cbComissao;
    Button btnNovoCliente;
    int posicaoSelecionada;
    Double totalGeral = 0.0;
    Double precos = 0.0;
    int posicaoSpinner;

    private ProgressDialog progressDialog;
    private android.os.Handler handler;

    String qtdSalsichas[] = {"Uma salsicha (3,50)",
            "Duas salsicha (4,00)"};
    ArrayAdapter<String> array_qtdSalsichas;

    ArrayList<String> itens = new ArrayList<String>();
    ArrayAdapter<String> adapterList;

    Vector<Double> valores = new Vector<Double>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cardapio);

        txtCardapio = (TextView) findViewById(R.id.txtCardapio);
        radioGroup = (RadioGroup) findViewById(R.id.RadioGroup);
        rbCachorroQuente = (RadioButton) findViewById(R.id.rbCachorroQuente);
        rbBauruSimples = (RadioButton) findViewById(R.id.rbBauruSimples);
        rbBauruOvo = (RadioButton) findViewById(R.id.rbBauruOvo);
        txt350 = (TextView) findViewById(R.id.txt350);
        txt400 = (TextView) findViewById(R.id.txt400);
        txt450 = (TextView) findViewById(R.id.txt450);
        spinner = (Spinner) findViewById(R.id.spinner);
        imageView = (ImageView) findViewById(R.id.imageView);
        txtNomeCliente = (TextView) findViewById(R.id.txtNomeCliente);
        edtNomeCliente = (EditText) findViewById(R.id.edtNomeCliente);
        txtQuantidade = (TextView) findViewById(R.id.txtQuantidade);
        edtQuantidade = (EditText) findViewById(R.id.edtQuantidade);
        btnInclirConta = (Button) findViewById(R.id.btnIncluirConta);
        lvLista = (ListView) findViewById(R.id.lvLista);
        txtTotal = (TextView) findViewById(R.id.txtTotal);
        edtTotal = (EditText) findViewById(R.id.edtTotal);
        cbComissao = (CheckBox) findViewById(R.id.cbComissao);
        btnNovoCliente = (Button) findViewById(R.id.btnNovoCliente);

        handler = new android.os.Handler();

        imageView.setImageResource(R.drawable.cachorroquente);
        imageView.getLayoutParams().width = 300;
        imageView.getLayoutParams().height = 200;


        array_qtdSalsichas = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, qtdSalsichas);
        spinner.setAdapter(array_qtdSalsichas);


        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                Toast.makeText(parent.getContext(), "Você escolheu: " + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
                posicaoSpinner = pos;
            }

            public void onNothingSelected(AdapterView<?> arg0) {

            }
        });

        adapterList = new ArrayAdapter<String>(Cardapio.this, android.R.layout.simple_list_item_1, itens);
        lvLista.setAdapter(adapterList);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup grupo, int checkedId) {
                int botaoMarcado = grupo.getCheckedRadioButtonId();
                precos = 0.0;
                switch (botaoMarcado) {
                    case R.id.rbCachorroQuente:
                        if (posicaoSpinner == 0) {
                            precos = 3.50;
                        } else {
                            precos = 4.00;
                        }
                        imageView.setImageResource(R.drawable.cachorroquente);
                        imageView.getLayoutParams().width = 300;
                        imageView.getLayoutParams().height = 200;
                        break;
                    case R.id.rbBauruSimples:
                        precos = 4.00;
                        imageView.setImageResource(R.drawable.baurusimples);
                        imageView.getLayoutParams().width = 300;
                        imageView.getLayoutParams().height = 200;
                        break;
                    case R.id.rbBauruOvo:
                        precos = 4.50;
                        imageView.setImageResource(R.drawable.baurucomovo);
                        imageView.getLayoutParams().width = 300;
                        imageView.getLayoutParams().height = 200;
                        break;
                }

            }
        });

        edtNomeCliente.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (edtNomeCliente.getText().toString().length() == 0) {
                    Toast.makeText(getApplicationContext(), "Informe o nome do cliente!",
                            Toast.LENGTH_LONG).show();
                    edtNomeCliente.selectAll();
                    edtNomeCliente.requestFocus();
                    return;
                }
            }
        });


        btnInclirConta.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                String str = "";
                if (edtQuantidade.getText().toString().length() == 0) {
                    Toast.makeText(getApplicationContext(), "Informe a quantidade!",
                            Toast.LENGTH_LONG).show();
                    edtQuantidade.selectAll();
                    edtQuantidade.requestFocus();
                    return;
                }
                int quantidade = Integer.parseInt(String.valueOf(edtQuantidade.getText().toString()));
                if (rbCachorroQuente.isChecked()) {
                    str = "Cachorro Quente";
                    totalGeral += precos * quantidade;
                } else if (rbBauruSimples.isChecked()) {
                    str = "Bauru Simples";
                    totalGeral += precos * quantidade;
                } else if (rbBauruOvo.isChecked()) {
                    str = "Bauru com Ovo";
                    totalGeral += precos * quantidade;
                }

                if (cbComissao.isChecked())
                    edtTotal.setText(Double.toString(totalGeral * 1.1));
                else
                    edtTotal.setText(Double.toString(totalGeral));

                str = str + " qtde: " + String.valueOf(edtQuantidade.getText().toString()) + " Valor: " + String.valueOf(precos);
                itens.add(str);
                adapterList.notifyDataSetChanged();
                valores.addElement(new Double(precos * quantidade));
                rbCachorroQuente.setChecked(false);
                rbBauruOvo.setChecked(false);
                rbBauruSimples.setChecked(false);

            }

        });

        lvLista.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> adapter, View view, int pos,
                                    long id) {

                posicaoSelecionada = pos;
                AlertDialog.Builder dialogo = new AlertDialog.Builder(
                        Cardapio.this);
                dialogo.setIcon(android.R.drawable.ic_dialog_alert);
                dialogo.setTitle("Exlcusão");
                dialogo.setMessage("Deseja Excluir o registro?");
                dialogo.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        progressDialog = ProgressDialog.show(Cardapio.this, "Aguarde", "Processando...");

                        new Thread() {
                            public void run() {
                                try {
                                    // simulando um processo demorado
                                    Thread.sleep(1000);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                                handler.post(new Runnable() {
                                    public void run() {
                                        itens.remove(posicaoSelecionada);
                                        adapterList.notifyDataSetChanged();
                                        totalGeral -= Double.parseDouble(String.valueOf(valores.get(posicaoSelecionada)));
                                        valores.removeElementAt(posicaoSelecionada);
                                        if (cbComissao.isChecked())
                                            edtTotal.setText(Double.toString(totalGeral * 1.1));
                                        else
                                            edtTotal.setText(Double.toString(totalGeral));
                                    }
                                });
                                //desativa a barra
                                progressDialog.dismiss();
                            }
                        }.start();

                    }
                });

                dialogo.setNegativeButton("Cancelar", null);
                dialogo.show();
            }
        });

        cbComissao.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()

        {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                if (cbComissao.isChecked())
                    edtTotal.setText(Double.toString(totalGeral * 1.1));
                else
                    edtTotal.setText(Double.toString(totalGeral));

            }
        });

        btnNovoCliente.setOnClickListener(new View.OnClickListener()

        {
            @Override
            public void onClick(View view) {
                edtNomeCliente.setText("");
                edtQuantidade.setText("");
                lvLista.setAdapter(null);
                edtTotal.setText("");
                cbComissao.setChecked(false);
            }
        });


    }
}

1 answer

0

I believe that it is not possible to access the context, because you are calling a context from outside the Istener, which only exists outside the Istener

So instead of using in Context Cardapio.this, try as follows:

To display the Progress in Activity would be:

final Context contextDaAct = this.getContext(); //Fora do listener
lvLista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   //Código...
   ProgressDialog.show(contextDaAct, "Aguarde", "Processando...");
}

But if you want to display Progress within the dialog, try:

ProgressDialog.show(dialogo.getContext(), "Aguarde", "Processando...");

Browser other questions tagged

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