Doubt to create dialog box in android studio

Asked

Viewed 240 times

-1

Good afternoon! I’m having a doubt on android where I’m creating an app to evaluate the user’s mood . inserir a descrição da imagem aqui

I create the code in such a way that I accept only one option per day Only now I need to include a window informing that he has already performed the test if he has already done and I need that after he has answered open a dialog box to include personal information like phone name etc.. Below is the code made until the moment:

public class ActPrincipal extends Activity {
    private ImageButton ibMuitoSatisfeito, ibSatisfeito, ibNeutro, ibInsatisfeito, ibMuitoInsatisfeito;

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

        ibMuitoSatisfeito = (ImageButton) findViewById(R.id.ibMuitoInsatisfeito);
        ibSatisfeito = (ImageButton) findViewById(R.id.ibSatisfeito);
        ibNeutro = (ImageButton) findViewById(R.id.ibNeutro);
        ibInsatisfeito = (ImageButton) findViewById(R.id.ibInsatisfeito);
        ibMuitoInsatisfeito = (ImageButton) findViewById(R.id.ibMuitoInsatisfeito);

        ibMuitoSatisfeito.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                salvarAvaliacao(1);
                return true;
            }
        }
        );
        ibSatisfeito.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {               
                salvarAvaliacao(2);
                return true;
              }
          }
        ); ibNeutro.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                salvarAvaliacao(3);
                 return true;
             }
         }
        );
        ibMuitoInsatisfeito.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                salvarAvaliacao(5);
                return true;
            }
        }
        );
    }

    public void salvarAvaliacao(int nro) {
        Controlador controlador = new Controlador(getBaseContext());
        Avaliacao avaliacao = new Avaliacao();
        avaliacao.setDataAvaliacao(new SimpleDateFormat("dd/MM/yyyy").format(new Date()));
        avaliacao.setHorario(new SimpleDateFormat("HH:mm:ss").format(new Date()));
        avaliacao.setAvaliacao(nro);
        avaliacao.setEnviado(0);

        if (android.os.Build.VERSION.SDK_INT > 22) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 12);
        }
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

            Location localizacao = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);

            if (localizacao != null && ((new Date()).getTime() - localizacao.getTime()) / 1000 < 3600) {
                avaliacao.setLatitude(localizacao.getLatitude());
                avaliacao.setLongitude(localizacao.getLongitude());
            }
        }
        controlador.inserir(avaliacao);
        Toast.makeText(this, getString(R.string.msg_agradecimento), Toast.LENGTH_SHORT).show();
    }

    public void btAbrirActAvaliacao(View v) {
        startActivity(new Intent(this, ActAvaliacao.class));
    }
}

1 answer

2


I believe that logic can be greatly improved. As I analyzed part of your code and realized you are learning Android, I will suggest you follow this script (I will not propose you more accurate techniques):

2 Activities:

Testehumor (that appears the options to vote). Personal data (with a layout with name, phone and email).

The main Activity would be Testehumor. It is the MAIN application Activity. When the user accesses to vote, you check whether you have already voted today (by Sqlite bank, Sharedpreferences... the medium does not matter). If you have already voted, you can have in this Activity a layout only with textview stating that he has already voted. If you haven’t voted, load the voting layout layouts in the same XML. One displays (setVisibility) and the other you hide). Within the same XML layout you can work by displaying one and hiding the other (but it is not always that this is recommended). After he has voted (if it is the first time, you carry the).

Always try to revise your question to make it clear and concise. Very general questions or asking for full codes (solutions to your problem) can harm your reputation. Always try to be specific and study before asking, because someone has already given a "-1" in their doubt.

A hug and I hope I helped.

Browser other questions tagged

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