【问题标题】:Radio group button always saving the default value into sqlite单选组按钮始终将默认值保存到 sqlite
【发布时间】:2016-04-06 11:31:42
【问题描述】:

我的问题是,每当我检查数据库以查看患者时,即使我检查了女性,我也看到性别是男性。 这是代码: 片段患者.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();
                                               }
                }
                                       );

    }   
}

请帮忙解决我的问题

【问题讨论】:

  • 你在哪里看到的性别?
  • 在使用 firefox 的 sqlite 管理器中

标签: java android xml sqlite


【解决方案1】:

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

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

            }


        }
    });

【讨论】:

  • thx 但是当我添加此代码时,v 带有下划线,它告诉我:无法在以不同方法定义的内部类中引用非最终变量 v
  • 声明 v 为最终变量。
  • 最终视图 v = inflater.inflate(R.layout.fragment_patient, parent, false);
  • 不幸的是应用程序崩溃并给我这个:01-02 04:47:19.986: E/AndroidRuntime(1218): Process: com.example.apppointapp, PID: 1218 01-02 04:47 :19.986: E/AndroidRuntime(1218): java.lang.NullPointerException 01-02 04:47:19.986: E/AndroidRuntime(1218): at com.example.apppointapp.PatientFragment$2.onClick(PatientFragment.java:88)
【解决方案2】:

解决方案:1

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

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. 将其与您在onClick() 中选择的RadioButton 进行比较

    @Override
    public void onClick(View v){
    
      int selectedId = radioG.getCheckedRadioButtonId();
      if(selectedId == radio1.getId()) {    
          selectedRadioButton = radio1;
      }
      else if(selectedId == radio2.getId()) {    
          selectedRadioButton = radio2;
      }
    

【讨论】:

  • 但是您应该检查它在哪里崩溃以及为什么?
  • 这里是第一个解决方案的 logcat 输出:01-02 06:04:06.626: E/AndroidRuntime(1599): FATAL EXCEPTION: main 01-02 06:04:06.626: E/AndroidRuntime (1599): 进程: com.example.appointapp, PID: 1599 01-02 06:04:06.626: E/AndroidRuntime(1599): java.lang.NullPointerException 01-02 06:04:06.626: E/AndroidRuntime(1599 ): 在 com.example.apppointapp.PatientFragment$1.onClick(PatientFragment.java:83) 01-02 06:04:06.626: E/AndroidRuntime(1599): 在 android.view.View.performClick(View.java:4438 )
猜你喜欢
  • 2021-05-25
  • 1970-01-01
  • 2014-06-07
  • 2020-01-21
  • 2017-04-05
  • 2016-04-17
  • 1970-01-01
  • 2019-08-19
  • 1970-01-01
相关资源
最近更新 更多