RadioButton值未保存到数据库

时间:2015-06-05 11:22:20

标签: android radio-button android-sqlite android-database

在下面的代码中,radiobutton值未保存到数据库中。当我调试它时显示为null。

是否需要为radiobuttons设置文本或使用textview名称?我们可以保存radiobuttons的值。

这是我的布局设计:

XML

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

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="2dp"
        android:layout_marginTop="20dp"
        android:padding="5dp"
        android:text="Excellent"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="15sp"
        android:textStyle="bold" />
     <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="2dp"
        android:layout_marginTop="20dp"
        android:padding="5dp"
        android:text="Good"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="15sp"
        android:textStyle="bold" />
    TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="2dp"
        android:layout_marginTop="20dp"
        android:padding="5dp"
        android:text="Satisfactory"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="15sp"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="2dp"
        android:layout_marginTop="20dp"
        android:padding="5dp"
        android:text="Poor"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="15sp"
        android:textStyle="bold" />


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

    <TextView
       android:id="@+id/textView10"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginLeft="2dp"
       android:layout_marginTop="20dp"
       android:padding="5dp"
       android:text="Appointment/ Admission"
       android:textAppearance="?android:attr/textAppearanceSmall"
       android:textSize="15sp"
       android:textStyle="bold" />

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

    <RadioGroup
        android:id="@+id/customer_appointment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Excellent"/>
            <RadioButton
                android:id="@+id/radioButton2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="40dp"/>

            <RadioButton
                android:id="@+id/radioButton3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="40dp"/>
             <RadioButton
                android:id="@+id/radioButton4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="30dp"/>

        </RadioGroup>
</LinearLayout>

爪哇

public class Feedback extends Activity {
    TextView tv1;
    Button back, home, next, save;
    EditText first_name1, last_name1, email1, first_learn1, mobile_no1;
    RadioGroup customer_appointment1, customer_information1, customer_telephone1, customer_waitngtime1, diagonsis_radilogy1,
    diagonsis_lab1, diagonsis_pharmarcy1, diagonsis_physi1, diagonsis_waitngtime1, nursing_quality1, nursing_expla1, doctor_expla1, doctor_personal1, clinics_expla1,
    clinics_process1, security_helpfull1, staff_friendliness1, staff_handling_problems1, staff_promptness1,rating1;

    RadioButton rb;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.feedback);
        first_name1 = (EditText)findViewById(R.id.first_name);
        last_name1 = (EditText)findViewById(R.id.last_name);
        email1 = (EditText)findViewById(R.id.email);
        mobile_no1 = (EditText)findViewById(R.id.mobile_no);
        first_learn1 = (EditText)findViewById(R.id.first_learn);

        customer_appointment1 = (RadioGroup)findViewById(R.id.customer_appointment);
        // int selectedId = customer_appointment1.getCheckedRadioButtonId();
        // rb = (RadioButton) findViewById(selectedId);
        customer_information1 = (RadioGroup)findViewById(R.id.customer_information);
        customer_telephone1 = (RadioGroup)findViewById(R.id.customer_telephone);
        customer_waitngtime1 = (RadioGroup)findViewById(R.id.customer_waitngtime);
        diagonsis_radilogy1 = (RadioGroup)findViewById(R.id.diagonsis_radilogy);
        diagonsis_lab1 = (RadioGroup)findViewById(R.id.diagonsis_lab);
        diagonsis_pharmarcy1 = (RadioGroup)findViewById(R.id.diagonsis_pharmarcy);
        diagonsis_physi1 = (RadioGroup)findViewById(R.id.diagonsis_physi);
        diagonsis_waitngtime1 = (RadioGroup)findViewById(R.id.diagonsis_waitngtime);
        nursing_quality1 = (RadioGroup)findViewById(R.id.nursing_quality);
        nursing_expla1 = (RadioGroup)findViewById(R.id.nursing_expla);
        doctor_expla1 = (RadioGroup)findViewById(R.id.doctor_expla);
        doctor_personal1 = (RadioGroup)findViewById(R.id.doctor_personal);
        clinics_expla1 = (RadioGroup)findViewById(R.id.clinics_expla);
        clinics_process1 = (RadioGroup)findViewById(R.id.clinics_process);
        security_helpfull1 = (RadioGroup)findViewById(R.id.security_helpfull);
        staff_friendliness1 = (RadioGroup)findViewById(R.id.staff_friendliness);
        staff_handling_problems1 = (RadioGroup)findViewById(R.id.staff_handling_problems);
        staff_promptness1 = (RadioGroup)findViewById(R.id.staff_promptness);
        // rating = (RadioGroup)findViewById(R.id.rating);

        next = (Button)findViewById(R.id.next);

        next.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            switch (v.getId()) {
                case R.id.next:
                    setContentView(R.layout.feedback2);
                    save = (Button)findViewById(R.id.save);
                    save.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {

                            String first_name = first_name1.getText().toString();
                            String last_name = last_name1.getText().toString();
                            String email = email1.getText().toString();
                            String mobile_no = mobile_no1.getText().toString();
                            String first_learn = first_learn1.getText().toString();
                            String customer_appointment = ((RadioButton)findViewById(customer_appointment1.getCheckedRadioButtonId())).getText().toString();
                            String customer_information = ((RadioButton)findViewById(customer_information1.getCheckedRadioButtonId())).getText().toString();
                            String customer_telephone = ((RadioButton)findViewById(customer_telephone1.getCheckedRadioButtonId())).getText().toString();
                            String customer_waitngtime = ((RadioButton)findViewById(customer_waitngtime1.getCheckedRadioButtonId())).getText().toString();
                            String diagonsis_radilogy = ((RadioButton)findViewById(diagonsis_radilogy1.getCheckedRadioButtonId())).getText().toString();
                            String diagonsis_lab = ((RadioButton)findViewById(diagonsis_lab1.getCheckedRadioButtonId())).getText().toString();
                            String diagonsis_pharmarcy = ((RadioButton)findViewById(diagonsis_pharmarcy1.getCheckedRadioButtonId())).getText().toString();
                            String diagonsis_physi = ((RadioButton)findViewById(diagonsis_physi1.getCheckedRadioButtonId())).getText().toString();
                            String diagonsis_waitngtime = ((RadioButton)findViewById(diagonsis_waitngtime1.getCheckedRadioButtonId())).getText().toString();
                            String nursing_quality = ((RadioButton)findViewById(nursing_quality1.getCheckedRadioButtonId())).getText().toString();
                            String nursing_expla = ((RadioButton)findViewById(nursing_expla1.getCheckedRadioButtonId())).getText().toString();
                            String doctor_expla = ((RadioButton)findViewById(doctor_expla1.getCheckedRadioButtonId())).getText().toString();
                            String doctor_personal = ((RadioButton)findViewById(doctor_personal1.getCheckedRadioButtonId())).getText().toString();
                            String clinics_expla = ((RadioButton)findViewById(clinics_expla1.getCheckedRadioButtonId())).getText().toString();
                            String clinics_process = ((RadioButton)findViewById(clinics_process1.getCheckedRadioButtonId())).getText().toString();
                            String security_helpfull = ((RadioButton)findViewById(security_helpfull1.getCheckedRadioButtonId())).getText().toString();
                            String staff_friendliness = ((RadioButton)findViewById(staff_friendliness1.getCheckedRadioButtonId())).getText().toString();
                            String staff_handling_problems = ((RadioButton)findViewById(staff_handling_problems1.getCheckedRadioButtonId())).getText().toString();
                            String staff_promptness = ((RadioButton)findViewById(staff_promptness1.getCheckedRadioButtonId())).getText().toString();
                            // String rating = ((RadioButton)findViewById(rating1.getCheckedRadioButtonId())).getText().toString();

                            try {
                                String queryString ="first_name="+ first_name+"&last_name="+last_name+"&email="+email+ "&mobile_no="+mobile_no
                                        +"&first_learn="+first_learn+"&customer_appointment="+customer_appointment+"&customer_information="+customer_information
                                         +"&customer_telephone="+customer_telephone+"&customer_waitngtime="+customer_waitngtime+"&diagonsis_radilogy="+diagonsis_radilogy+"&diagonsis_lab="+diagonsis_lab
                                         +"&diagonsis_pharmarcy="+diagonsis_pharmarcy+"&diagonsis_physi="+diagonsis_physi
                                         +"&diagonsis_waitngtime="+diagonsis_waitngtime+"&nursing_quality="+nursing_quality+"&nursing_expla="+nursing_expla
                                         +"&doctor_personal="+doctor_personal+"&clinics_expla="+clinics_expla+"&clinics_process="+clinics_process
                                         +"&security_helpfull="+security_helpfull+"&staff_friendliness="+staff_friendliness+"&staff_handling_problems="+staff_handling_problems+"&staff_promptness="+staff_promptness;
                                String data = DatabaseUtility.executeQueryPhp("feedback", queryString);
                                System.out.print(data);

                                /* fname1.setText("");
                                   lname1.setText("");
                                   mobile1.setText("");
                                   altmob1.setText("");
                                   email1.setText("");
                                   comment1.setText("");
                                */

                                Toast.makeText(
                                        Feedback.this,
                                        "Message:Records Saved Sucessfully",
                                        Toast.LENGTH_SHORT).show();
                            }
                            catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });

                    break;

                default:
                    setContentView(R.layout.feedback);
                    break;
            }
        }
    });
}

1 个答案:

答案 0 :(得分:0)

更改您的代码并查看调试到appointment_rbcustomer_appointment值。

RadioButton appointment_rb = (RadioButton) findViewById(customer_appointment1.getCheckedRadioButtonId());
String customer_appointment = (String) appointment_rb.getText();

null在哪里进行?

    <RadioButton
                  android:id="@+id/radioButton2"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content" 
                  android:layout_marginLeft="40dp"/>

我看不到此按钮的文字。如果选择它,则文本将为空。

可能是用户没有选择任何按钮?

<强> PS

next.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.next: 

开关没用,&#39; v&#39;永远是&#39; next&#39;

<强> UPD

使用以下方法获取已检查的单选按钮ID,如果返回-1,则表示用户未选择任何单选按钮。

 radioGroup.getCheckedRadioButtonId(); // it will return unique ID of checked radio button or -1 on empty selection.

请参阅此处的文档&gt;&gt; getCheckedRadioButtonID();

最后,这个字符串

  

的setContentView(R.layout.feedback2);

为了什么?试着评论一下。