THIS IS THE CONTINUATION OF MY QUESTION:
I have a problem with getting something from the previous activity of my application. My case is, the previous activity, known as ListOfMeals, it has a listview (breakfast, morning snack, etc.) If I’d click breakfast and add meal, I’d click for list of foods like bread, fruits, veggies, etc. After that, it should add to my database. But what I have so far is this, please refer below:
case R.id.btFoodVegetableSave:
String mealname = selected;
String serving = calories.getText().toString();
int i = Integer.parseInt(serving.replaceAll("[\\D]", ""));
String servng = String.valueOf(i);
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
String strDate = sdf.format(new Date());
if ( ( mealname.isEmpty() || servng.isEmpty() ) ){
// call for custom toast
viewErrorToast();
}
else {
boolean didItWork = true;
try{
BreakFastLog entry = new BreakFastLog(Baguettes_Bagels.this);
entry.open();
entry.createEntry(mealname, servng, strDate);
entry.close();
}
catch(Exception e){
didItWork = false;
viewErrorToast();
}finally{
if (didItWork){
viewBMRSavedToast();
}
}
} // end of if else statement
break;
The structure of my app is like this:
Activity A – List of Meals (listview = Breakfast, Morning Snack, etc)
Activity B – Selected from Activity A (e.g., Morning Snack) where it has a button to add more food
Activity C – List of meal categories (listview again: bread, pasta, etc)
Activity D – Selected from Activity C (listview again: let’s assume the user selected bread)
Activity E – list of breads (it has listview again)
Activity F – Selected from Activity E – (the user now selects for example white bread)
activity F has a save button, now that the user based on my example, selected morning snack, the data from activity f should be saved to morning snack table. How can I do this?
In my example, it only saves to my breakfast. But problem rises here when I’d choose morning snack not the breakfast. How can I get what the user selected from a previous activity and save it to its respective meal?
I know this has got something to do with this:
BreakFastLog entry = new BreakFastLog(Baguettes_Bagels.this);
entry.open();
entry.createEntry(mealname, servng, strDate);
entry.close();
I have to put if else statement, but I can’t think of some clever ways to implement this. Thanks for your help in advance.
NOTE:
I’ve tried doing this:
Bundle extras = getIntent().getExtras();
String breakfast = this.getIntent().getStringExtra("key_breakfast");
String ms = this.getIntent().getStringExtra("key_morningsnack");
String lunch = this.getIntent().getStringExtra("key_lunch");
String as = this.getIntent().getStringExtra("key_afternoonsnack");
String dinner = this.getIntent().getStringExtra("key_dinner");
String es = this.getIntent().getStringExtra("key_eveningsnack");
if( extras.equals(breakfast)){
BreakFastLog entry = new BreakFastLog(Baguettes_Bagels.this);
entry.open();
entry.createEntry(mealname, servng, strDate);
entry.close();
}
else if( extras.equals(ms) ){
MorningSnackLog entry = new MorningSnackLog(Baguettes_Bagels.this);
entry.open();
entry.createEntry(mealname, servng, strDate);
entry.close();
}
else if( extras.equals(lunch) ){
LunchLog entry = new LunchLog(Baguettes_Bagels.this);
entry.open();
entry.createEntry(mealname, servng, strDate);
entry.close();
}
else if( extras.equals(as) ){
AfternoonSnackLog entry = new AfternoonSnackLog(Baguettes_Bagels.this);
entry.open();
entry.createEntry(mealname, servng, strDate);
entry.close();
}
else if( extras.equals(dinner) ){
DinnerLog entry = new DinnerLog(Baguettes_Bagels.this);
entry.open();
entry.createEntry(mealname, servng, strDate);
entry.close();
}
else if( extras.equals(es) ){
EveningSnackLog entry = new EveningSnackLog(Baguettes_Bagels.this);
entry.open();
entry.createEntry(mealname, servng, strDate);
entry.close();
}
to no avail. When i ran this and click the save button it is caught by my exception handler.
will never return true. You are comparing a bundle to a string in this case. There are also some issues in the way you are setting up the bundle. Your bundle really only needs one property, the meal type (KEY_MEAL below). Try something like this…
and in the next activity…