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;
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.
When you start the activity you have probable something like:
You can add:
And when your second activity starts you can retreive it with in the onCreate for example: