code fragment of what I’ve tried:
String servng, mealname, strDate;
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.btFoodVegetableSave:
mealname = selected;
String serving = calories.getText().toString();
int i = Integer.parseInt(serving.replaceAll("[\\D]", ""));
servng = String.valueOf(i);
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
strDate = sdf.format(new Date());
if ( ( mealname.isEmpty() || servng.isEmpty() ) ){
// call for custom toast
viewErrorToast();
}
else {
boolean didItWork = true;
try{
rgMeal.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch( checkedId ){
case R.id.rbBreakfast:
BreakFastLog bfLog = new BreakFastLog(Bread_Dark.this);
bfLog.open();
bfLog.createEntry(mealname, servng, strDate);
bfLog.close();
break;
case R.id.rbMorningSnack:
MorningSnackLog msLog = new MorningSnackLog(Bread_Dark.this);
msLog.open();
msLog.createEntry(mealname, servng, strDate);
msLog.close();
break;
case R.id.rbLunch:
LunchLog lunchLog = new LunchLog(Bread_Dark.this);
lunchLog.open();
lunchLog.createEntry(mealname, servng, strDate);
lunchLog.close();
break;
case R.id.rbAfternoonSnack:
AfternoonSnackLog asLog = new AfternoonSnackLog(Bread_Dark.this);
asLog.open();
asLog.createEntry(mealname, servng, strDate);
asLog.close();
break;
case R.id.rbDinner:
DinnerLog dinnerLog = new DinnerLog(Bread_Dark.this);
dinnerLog.open();
dinnerLog.createEntry(mealname, servng, strDate);
dinnerLog.close();
break;
case R.id.rbEveningSnack:
EveningSnackLog esLog = new EveningSnackLog(Bread_Dark.this);
esLog.open();
esLog.createEntry(mealname, servng, strDate);
esLog.close();
break;
}
}
});
}
catch(Exception e){
didItWork = false;
viewErrorToast();
}finally{
if (didItWork){
viewBMRSavedToast();
}
}
} // end of if else statement
break;
the xml layout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="481dp"
android:background="@drawable/ihealthfirst"
android:orientation="vertical" >
<include
android:layout_width="fill_parent"
android:layout_height="match_parent"
layout="@layout/tabs_menu" />
<TextView
android:id="@+id/tvFoodVegetable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cabbage"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/etAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="@string/one"
android:inputType="number" >
<requestFocus android:layout_height="wrap_content" />
</EditText>
<Spinner
android:id="@+id/spFoodVegetable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:prompt="@string/white_bread_prompt" />
</LinearLayout>
<Button
android:id="@+id/btFoodVegetableCalories"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/done"
android:layout_margin="15dp"
android:textColor="@color/darkgreen"
android:textSize="@dimen/padding_extralarge"
android:background="@drawable/btn_meal_selected" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text="@string/choosemeal"
android:textAppearance="?android:attr/textAppearanceLarge" />
<RadioGroup
android:paddingLeft="10dp"
android:id="@+id/rgSelectMeal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioButton
android:id="@+id/rbBreakfast"
android:layout_width="128dp"
android:layout_height="wrap_content"
android:text="@string/breakfast"
android:checked="true" />
<RadioButton
android:id="@+id/rbMorningSnack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/morningsnack" />
<RadioButton
android:id="@+id/rbLunch"
android:layout_width="128dp"
android:layout_height="wrap_content"
android:text="@string/lunch" />
<RadioButton
android:id="@+id/rbAfternoonSnack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/afternoonsnack" />
<RadioButton
android:id="@+id/rbDinner"
android:layout_width="127dp"
android:layout_height="wrap_content"
android:text="@string/dinner" />
<RadioButton
android:id="@+id/rbEveningSnack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/eveningsnack" />
</RadioGroup>
<Button
android:id="@+id/btFoodVegetableSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/save"
android:drawableLeft="@drawable/save24"
android:background="@drawable/nutrition_button_gradiant"
android:textColor="@color/white"
android:layout_gravity="center"
android:layout_marginBottom="15dp" />
</LinearLayout>
When the user would check from radio group and click the save button, it goes to my FINALLY statement:
finally{
if (didItWork){
viewBMRSavedToast();
}
But when i would check the summary (list of records inserted/saved to the database), it’s not being saved 🙁 what’s wrong with my handling of radiogroup here?
If I understand you correctly you want the user to click on a radio button and then to create a new BreakFastLog when the user presses “Save”. What you’re currently doing is making a checkChangedListener for the radiogroup after the “Save” button has been clicked. This will cause the chain of events to happen in inverse order from what you want (what you’re seeing). In order for the user to select a radiobutton and then click “save” you only need to retrieve the currently checked radiobutton within the radiogroup, not make a checkChangedListener. This can be done by:
`