2013-01-24 33 views
0

什么,我已经试过的代码片段:ANDROID radioGroup中不保存oncheckedchangeListener

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; 

的XML布局:

<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> 

当用户将检查从单选按钮组,并单击保存按钮,它会进入我的FINALLY声明:

finally{ 
      if (didItWork){ 
       viewBMRSavedToast(); 
      } 

但是,当我检查总结(记录列表插入/保存到数据库),它不会被保存:(我在这里处理radiogroup有什么问题?

+0

你告诉你radioGroup中听重写onclick事件:这可以这样做? radiogroups.setonlicklistener(本); – EdGs

+0

是的,这是我到目前为止所尝试的。 –

回答

1

如果我理解正确,您希望用户单击单选按钮,然后在用户按下“保存”时创建新的BreakFastLog。你目前正在做的是在之后为“Radius”制作checkChangedListener,点击“保存”按钮。这将导致事件链发生的顺序与你想要的相反(你所看到的)。为了让用户选择一个单选按钮,然后单击“保存”,您只需要检索radiogroup中当前检查的单选按钮,而不是生成checkChangedListener。

int checkedId = rgMeal.getCheckedRadioButtonId() 
    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); 
        . 
        . 
        . 
        . 
    } 

`