I want to show selected checkbox values with calories to another activity when button clicked in Android Studio. And show total calorie intake in editText in second activity. I want to show them with textView in second activity. I am trying to pass checked values with their calories to another activity(it's textview.)
Could you help me, how can I do that?. :(
This is my first class :
'''
public class Nutrition extends AppCompatActivity {
FloatingActionButton buttonNext ;
FloatingActionButton buttonBack ;
CheckBox apple ;
CheckBox egg ;
CheckBox pasta ;
CheckBox pizza ;
CheckBox broccoli ;
CheckBox banana ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nutrition);
apple = (CheckBox) findViewById(R.id.apple);
egg = (CheckBox) findViewById(R.id.egg);
pasta = (CheckBox) findViewById(R.id.pasta);
pizza = (CheckBox) findViewById(R.id.pizza);
broccoli = (CheckBox) findViewById(R.id.broccoli);
banana = (CheckBox) findViewById(R.id.banana);
buttonNext = (FloatingActionButton) findViewById(R.id.btnNext);
buttonNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Nutrition.this, foods.class);
intent.putExtra("Apple", apple.isChecked());
intent.putExtra("Egg", egg.isChecked());
intent.putExtra("Pasta", pasta.isChecked());
intent.putExtra("Pizza", pizza.isChecked());
intent.putExtra("Broccoli", broccoli.isChecked());
intent.putExtra("Banana", banana.isChecked());
startActivity(intent);
}
});
}}'''
This is my second class :
'''
public class foods extends AppCompatActivity {
TextView txtFood;
EditText txtTotalIntake ;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_foods);
Intent intent = getIntent();
Boolean apple = this.getIntent().getExtras().getBoolean("Apple", false);
Boolean egg = this.getIntent().getExtras().getBoolean("Egg", false);
Boolean pasta = this.getIntent().getExtras().getBoolean("Pasta", false);
Boolean pizza = this.getIntent().getExtras().getBoolean("Pizza", false);
Boolean broccoli = this.getIntent().getExtras().getBoolean("Broccoli", false);
Boolean banana = this.getIntent().getExtras().getBoolean("Banana", false);
}
}'''
CodePudding user response:
you can try like this-
public class foods extends AppCompatActivity {
TextView txtFood;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_foods);
Intent intent = getIntent();
Boolean apple = this.getIntent().getExtras().getBoolean("Apple", false);
Boolean egg = this.getIntent().getExtras().getBoolean("Egg", false);
Boolean pasta = this.getIntent().getExtras().getBoolean("Pasta", false);
Boolean pizza = this.getIntent().getExtras().getBoolean("Pizza", false);
Boolean broccoli = this.getIntent().getExtras().getBoolean("Broccoli", false);
Boolean banana = this.getIntent().getExtras().getBoolean("Banana", false);
String food="";
if(apple){
food ="Apple ";
}
if(egg){
food ="Egg "
}
if(pasta){
food ="Pasta";
}
if (pizza) {
food = "Pizza ";
}
if (broccoli) {
food = "Broccoli ";
}
if (banana) {
food = "Banana ";
}
txtFood.setText(food);
}
}
CodePudding user response:
Here's a clear solution,
public class foods extends AppCompatActivity {
TextView txtFood;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_foods);
Intent intent = getIntent();
Boolean apple = this.getIntent().getExtras().getBoolean("Apple", false);
Boolean egg = this.getIntent().getExtras().getBoolean("Egg", false);
Boolean pasta = this.getIntent().getExtras().getBoolean("Pasta", false);
Boolean pizza = this.getIntent().getExtras().getBoolean("Pizza", false);
Boolean broccoli = this.getIntent().getExtras().getBoolean("Broccoli", false);
Boolean banana = this.getIntent().getExtras().getBoolean("Banana", false);
//Newly added code
String food = "";
if (apple) {
food = "Apple ";
}
if (egg) {
food = "Egg ";
}
if (pasta) {
food = "Pasta ";
}
if (pizza) {
food = "Pizza ";
}
if (broccoli) {
food = "Broccoli ";
}
if (banana) {
food = "Banana ";
}
}
txtFood.setText(food);
}