samedi 2 janvier 2016

Radio group button always saving the default value into sqlite

My problem is that whenever I checked database to see patients, I see that the gender is male even if I checked female. Here is the code: Fragment patient.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
    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();
                                               }
                }
                                       );

    }   
}

Please help to solve my problem

Aucun commentaire:

Enregistrer un commentaire