I created a RadioGroup to enable the gender selection.
Here's the part of the XML file that declares the RadioGroup and the RadioButtons.
<RadioGroup
android:id="@+id/rgGenderGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rbMale"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="@string/m"
android:textColor="@color/default_text"/>
<RadioButton
android:id="@+id/rbFemale"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="@string/f"
android:textColor="@color/default_text"/>
</RadioGroup>
And now, in the activity, I declared the RadioGroup and RadioButton variables and overrode the onCheckedChanged method. To return a certain value of each choice, I also declared a String value.
private String gender = "";
private RadioGroup rgGenderGroup;
private RadioButton rbMale, rbFemale;
...
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId) {
case(R.id.rbMale):
gender = "M";
break;
case(R.id.rbFemale):
gender = "F";
break;
}
}
But, as I tried to insert the gender value into the database with the query below,
private final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " ( " +
COLUMN_NAME + " TEXT NOT NULL, " + COLUMN_AGE + " INT, " + COLUMN_NATIONALITY + " TEXT NOT NULL, " +
COLUMN_GENDER + " CHAR(1) NOT NULL, " + COLUMN_CREATED_AT + " TEXT NOT NULL);";
It fails, showing the following error message.
08-10 23:46:52.356 21666-21890/com.marshall.sortingtester E/SQLiteDatabase﹕ Error inserting Name=clara Age=0 Nationality=Angola Created_At=2015/08/10 23:46:52 Gender=null
android.database.sqlite.SQLiteConstraintException: People.Gender may not be NULL (code 19)
Why is the gender value returning null even after checking the button properly?
EDIT: Here's the insertData method.
public void insertData(Person person) {
ContentValues values = new ContentValues();
values.put(SQLiteHandler.COLUMN_NAME, person.getName());
values.put(SQLiteHandler.COLUMN_AGE, person.getAge());
values.put(SQLiteHandler.COLUMN_NATIONALITY, person.getNationality());
values.put(SQLiteHandler.COLUMN_GENDER, person.getGender());
values.put(SQLiteHandler.COLUMN_CREATED_AT, person.getCreated_at());
db.insert(SQLiteHandler.TABLE_NAME, null, values);
}
Aucun commentaire:
Enregistrer un commentaire