单选按钮组始终将默认值保存到sqlite中

时间:2016-01-02 09:12:22

标签: java android xml sqlite

我的问题是每当我检查数据库看病人时,即使检查了女性,我也看到性别是男性。  这是代码: 片段patient.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="54dp"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/name_text"
            android:layout_width="143dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:ems="10"
            android:hint="@string/name_text_hint" />

        <EditText
            android:id="@+id/familyname_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:hint="@string/familyname_text_hint" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/date_text"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:ems="10"
            android:hint="@string/date_text_hint" >
        </EditText>
    </LinearLayout>

        <RadioGroup
        android:id="@+id/myRadioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text"
        android:checkedButton="@+id/male_text" >


        <RadioButton
            android:id="@+id/female_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/female_text" />

        <RadioButton
            android:id="@+id/male_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/male_text" />
    </RadioGroup>


    <EditText
        android:id="@+id/address_text"
        android:layout_width="348dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/address_text_hint" />

    <LinearLayout
        android:layout_width="340dp"
        android:layout_height="57dp"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/phone_text"
            android:layout_width="143dp"
            android:layout_height="wrap_content"
            android:hint="@string/phone_text_hint" />

        <EditText
            android:id="@+id/email_text"
            android:layout_width="213dp"
            android:layout_height="wrap_content"
            android:hint="@string/email_text_hint" />
    </LinearLayout>

    <Button
        android:id="@+id/save_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="@string/save_button" />

</LinearLayout>

Patientfragment.java

package com.example.appointapp;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class PatientFragment extends Fragment {
    DatabaseHelper myDB;
    EditText editName,editFamilyname,editDob,editAddress,editPhonenb,editEmail;
    Button btnSave, selectedRadioButton; 
    RadioButton radioB;
    RadioGroup radioG;
    private Patient mPatient;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPatient= new Patient();

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
    View v = inflater.inflate(R.layout.fragment_patient, parent, false);
    myDB = new DatabaseHelper(getActivity());
    editName = (EditText)v.findViewById(R.id.name_text);
    editFamilyname = (EditText)v.findViewById(R.id.familyname_text);
    editDob = (EditText)v.findViewById(R.id.date_text);
    editAddress = (EditText)v.findViewById(R.id.address_text);
    editPhonenb = (EditText)v.findViewById(R.id.phone_text);
    editEmail = (EditText)v.findViewById(R.id.email_text);
    btnSave = (Button)v.findViewById(R.id.save_button);
    radioG = (RadioGroup)v.findViewById(R.id.myRadioGroup);

    int selectedId = radioG.getCheckedRadioButtonId();
    if(selectedId != -1) {    
           selectedRadioButton = (RadioButton)v.findViewById(selectedId);

        }


    AddData();

    return v;
    }


    public void AddData() {
        btnSave.setOnClickListener(
                new View.OnClickListener(){
            @Override
                    public void onClick(View v){



            boolean isInserted = myDB.insertData(editName.getText().toString() ,
                        editFamilyname.getText().toString() ,
                        editDob.getText().toString() ,
                        editAddress.getText().toString() ,
                        editPhonenb.getText().toString() , 
                        editEmail.getText().toString(),
                        selectedRadioButton.getText().toString()) ;
            if(isInserted == true)
            Toast.makeText(getActivity(), "Patient inserted", Toast.LENGTH_LONG).show();
            else 
                Toast.makeText(getActivity(), "Patient not inserted", Toast.LENGTH_LONG).show();
                                               }
                }
                                       );

    }   
}

请帮助解决我的问题

2 个答案:

答案 0 :(得分:0)

您需要将检查更改侦听器添加到您的广播组

radioG.setOnCheckedChangeListener(new     RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            int selectedId = radioG.getCheckedRadioButtonId();
            if(selectedId != -1) {
                selectedRadioButton = (RadioButton)v.findViewById(selectedId);

            }


        }
    });

答案 1 :(得分:0)

解决方案:1

view onCreateView()对象声明为全局。

View view;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
view = inflater.inflate(R.layout.fragment_patient, parent, false);
    ...
}

,您的radioG.getCheckedRadioButtonId()代码应位于onClick()

@Override
public void onClick(View v){

   int selectedId = radioG.getCheckedRadioButtonId();
   if(selectedId != -1) {    
        selectedRadioButton = (RadioButton)view.findViewById(selectedId);
   }

   boolean isInserted = myDB.insertData(editName.getText().toString() ,
   editFamilyname.getText().toString() ,
   editDob.getText().toString() ,
   ....
 }

解决方案:2

  1. 创建您RadioButton
  2. 的对象
  3. 将其与RadioButton

    中选定的onClick()进行比较
    @Override
    public void onClick(View v){
    
      int selectedId = radioG.getCheckedRadioButtonId();
      if(selectedId == radio1.getId()) {    
          selectedRadioButton = radio1;
      }
      else if(selectedId == radio2.getId()) {    
          selectedRadioButton = radio2;
      }