0

我尝试过的代码片段:

    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();
            }

但是当我检查摘要(插入/保存到数据库的记录列表)时,它没有被保存:(我在这里处理无线电组有什么问题?

4

1 回答 1

0

如果我理解正确,您希望用户单击单选按钮,然后在用户按下“保存”时创建一个新的 BreakFastLog。您当前正在做的是在单击“保存”按钮后为无线电组创建一个 checkChangedListener 。这将导致事件链以与您想要的(您所看到的)相反的顺序发生。为了让用户选择一个单选按钮然后单击“保存”,您只需要在单选组中检索当前选中的单选按钮,而不是进行 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);
                   .
                   .
                   .
                   .
     }

`

于 2013-01-24T22:27:38.627 回答