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:
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:
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.
– Mr_Anderson
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
– Armando Marques Sobrinho
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!!!
– Boneco Sinforoso