0

私のアクティビティには、2 つのラジオ ボタンと、値を文字列変数「type」に保存する保存ボタンを備えた RadioGroup があります。最初のラジオ ボタンはタイプを「絶食」に初期化し、2 番目のボタンは「タイプ」を「非絶食」に初期化します。2 番目のオプションを選択すると、型が初期化されません。2 番目のボタンを機能させるには、最初のボタンを選択して [保存] をクリックする必要があります。次に、2 番目のオプションを再度選択すると、機能します。これがコードです。

糖尿病_オプション.xml

<RadioGroup
    android:id="@+id/do_radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/do_editText1"
    android:visibility="visible" >

    <RadioButton
        android:id="@+id/do_fasting"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fasting" />

    <RadioButton
        android:id="@+id/do_nonfasting"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Non Fasting" />
</RadioGroup>

DiabetesOptions.java (保存ボタンの onClick メソッド内)

public class DiabetesOptions extends Activity {

int typeFlag=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    rGroup=(RadioGroup)findViewById(R.id.do_radioGroup1);
}
//This is the method invoked if we click Sabe button
public void onSave(View view) {

    rGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // TODO Auto-generated method stub
            switch(checkedId){
            case R.id.do_fasting:
                type="Fasting";
                typeFlag=1;
                break;
            case R.id.do_nonfasting:
                type="Non Fasting";
                typeFlag=1;
                break;
            }
        }
    });

    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
    SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
    date = dateFormat.format(new Date());
    time = timeFormat.format(new Date());
    try {
        if(flag==1) {
        DiabetesDatabase entry = new DiabetesDatabase(this);
         if(typeFlag==1){
            entry.createEntry(date,time,type,level);
            Toast th = Toast.makeText(DiabetesOptions.this, "Entry Saved", Toast.LENGTH_SHORT);
            th.show();
         }
         else if(typeFlag==0){
            AlertDialog.Builder alertDialog=new AlertDialog.Builder(DiabetesOptions.this);
            alertDialog.setTitle("Alert!!!");
            alertDialog.setMessage("Select FASTING or NON FASTING option");
            alertDialog.setNeutralButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       dialog.dismiss();                        
                   }
               });
            alertDialog.show();
         }
        }

}
4

1 に答える 1