How to assign values to variables through Checkboxes and from there create specific Textviews in another Activity

Asked

Viewed 174 times

0

Guys, I would like to assign values to a variable according to the selection of a particular Checkbox in my project.

In an example. If the first checkbox was selected, the following variable would be assigned: cont+=1.

If the second checkbox was selected, the following variable would be assigned: cont+=2.

With this in the other Activity a condition was presented based on the value of the cont variable.

se cont==1 then a textview was created showing a certain text and se cont==2 another textview with another text appears:

inserir a descrição da imagem aqui

Now I would also like if selected a second checkbox (one of the two in the lower part, "employee" or "unemployed"), in the following Activity be created another textview appearing the following content:

inserir a descrição da imagem aqui

Notice that I do not serve a single textview with the 2 texts united (in sequence).

Codes:

Mainactivity.java

package genesysgeneration.stack;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    public int cont=0;
    private Button btnNext;
    private CheckBox cbCasado, cbSolteiro, cbEmpregado, cbDesempregado;

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

        btnNext=(Button)findViewById(R.id.btnNext);
        btnNext.setOnClickListener(this);

    }

    public void onClick(View v){

        Intent it = new Intent(this, Main2Activity.class);
        startActivity(it);

    }

}

Main2activity.java.

package genesysgeneration.stack;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Main2Activity extends AppCompatActivity {

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

Thanks in advance!!!

  • If you know beforehand the maximum and minimum amount of textViews you will use in the second Activity, then create them and only add some value to them when necessary. If you didn’t know how many textViews you would use, you would have to use a custom listview that would only receive textViews.

  • In case using the @Mr_anderson tip, if you do this, simply pass the status of the checkbox to the other activity and logically if you have checked Voce assigns the text values to the corresponding textviews to each one who has checked or not

  • I was able to use a textview as a reference, print it to be able to pass to the next Activity using "it.putExtra(..." Anyway, thank you very much!!!

1 answer

0


Another one that I discovered the answer shortly after I asked the question, but even after all this time no one answered.

Follow the solution I found and thought it best for my problem:

Main1.java:

package genesysgeneration.stack;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btnNext;
    private CheckBox cbCasado, cbSolteiro, cbEmpregado, cbDesempregado;
    private String estadoCivil, estadoEmprego;

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

        cbCasado=(CheckBox)findViewById(R.id.cbCasado);
        cbSolteiro=(CheckBox)findViewById(R.id.cbSolteiro);
        cbEmpregado=(CheckBox)findViewById(R.id.cbEmpregado);
        cbDesempregado=(CheckBox)findViewById(R.id.cbDesempregado);

        addCB();

        btnNext=(Button)findViewById(R.id.btnNext);
        btnNext.setOnClickListener(this);

    }

    private void addCB(){

        cbCasado.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (cbSolteiro.isChecked()){

                    cbSolteiro.setChecked(false);
                    estadoCivil="";

                }

                if (((CheckBox)v).isChecked()){

                    estadoCivil="Casado";

                }else {

                    estadoCivil="";

                }

            }
        });

        cbSolteiro.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (cbCasado.isChecked()){

                    cbCasado.setChecked(false);
                    estadoCivil="";

                }

                if (((CheckBox)v).isChecked()){

                    estadoCivil="Solteiro";

                }else {

                    estadoCivil="";

                }

            }
        });

        cbEmpregado.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (cbDesempregado.isChecked()){

                    cbDesempregado.setChecked(false);
                    estadoEmprego="";

                }

                if(((CheckBox)v).isChecked()){

                    estadoEmprego="Empregado";

                }else {

                    estadoEmprego="";

                }

            }
        });

        cbDesempregado.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (cbEmpregado.isChecked()){

                    cbEmpregado.setChecked(false);
                    estadoEmprego="";

                }

                if(((CheckBox)v).isChecked()){

                    estadoEmprego="Desempregado";

                }else {

                    estadoEmprego="";

                }

            }
        });

    }

    public void onClick(View v){

        Intent it = new Intent(this, Main2Activity.class);
        it.putExtra("estadoCivil", estadoCivil);
        it.putExtra("estadoEmprego", estadoEmprego);
        startActivity(it);

    }

}

I put a condition so that the user can not select at the same time married and single/ employee and unemployed... That when selecting the second checkbox, if the opposite checkbox is selected, it is unchecked:

if (cbSolteiro.isChecked()){

    cbSolteiro.setChecked(false);
    estadoCivil="";

}

Main2.java:

package genesysgeneration.stack;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class Main2Activity extends AppCompatActivity {

    private TextView tv01, tv02;
    private String estadoCivil, estadoEmprego;

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

        tv01=(TextView)findViewById(R.id.tv01);
        tv02=(TextView)findViewById(R.id.tv02);

        estadoCivil=getIntent().getStringExtra("estadoCivil");
        estadoEmprego=getIntent().getStringExtra("estadoEmprego");

        tv01.setText(String.valueOf("O sujeito é " + estadoCivil + "."));
        tv02.setText(String.valueOf("O sujeito é " + estadoEmprego + "."));

    }
}

You have to import the two strings exported from the previous Activity and ask Textviews to display them...

Browser other questions tagged

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