在SQLite数据库存储单选按钮值 [英] storing radio button value in sqlite database
问题描述
我有在这两个单选按钮单选按钮组。我想要得到的单选按钮的值,然后将其存储在database..how做呢?请帮助!我搜索了,但一切都是徒劳!
我想这code,但使用它后我的活动停止工作。
RG =(RadioGroup中)findViewById(R.id.radioGroup2);
如果(rg.getCheckedRadioButtonId()!= - 1)
{
INT ID = rg.getCheckedRadioButtonId();
查看单选按钮= rg.findViewById(ID);
INT radioid = rg.indexOfChild(单选)
单选按钮BTN =(单选)rg.getChildAt(radioid);
Father_spouse =(字符串)btn.getText();
}
-
如果你想存储的文本标签的
单选
然后用这样的://从radioGroup中选定的单选按钮
INT selectedId = radioGroup.getCheckedRadioButtonId();如果(selectedId!= -1){
//找到返回ID的单选按钮
selectedRadioButton =(单选)findViewById(selectedId); //你想要做什么用radioButtonText(它保存到数据库,你的情况)
串radioButtonText = selectedRadioButton.getText();
} -
如果你想保存一个布尔值等的
selectedId
测试你的单选按钮
和保存0或1到你的数据库列(的两个单选按钮实例启用/禁用更新的)://从radioGroup中选定的单选按钮
INT selectedId = radioGroup.getCheckedRadioButtonId();
布尔isAllowUpdate = FALSE;
开关(selectedId){
案例R.id.radioAllowUpdate:isAllowUpdate = TRUE;打破;
案例R.id.radioDisableUpdate:isAllowUpdate = FALSE;打破;
}//保存到数据库
如果(isAllowUpdate)
//真正的==>保存值1
其他
//虚假==>保存0值
编辑:
如果您应该控制选择的值,当它发送到数据库中,看到这个的教程
i have a radio group with two radio buttons in it. I want to get the value of the radio button and then store it in the database..how do i do that?? Pls help! I searched for it but all in vain! I tried this code but my activity stops working after using it
rg=(RadioGroup)findViewById(R.id.radioGroup2);
if(rg.getCheckedRadioButtonId()!=-1)
{
int id=rg.getCheckedRadioButtonId();
View radioButton=rg.findViewById(id);
int radioid=rg.indexOfChild(radioButton);
RadioButton btn = (RadioButton) rg.getChildAt(radioid);
Father_spouse=(String)btn.getText();
}
if you want to store the text label of your
RadioButton
then use this :// get selected radio button from radioGroup int selectedId = radioGroup.getCheckedRadioButtonId(); if(selectedId != -1) { // find the radiobutton by returned id selectedRadioButton = (RadioButton) findViewById(selectedId); // do what you want with radioButtonText (save it to database in your case) String radioButtonText = selectedRadioButton.getText(); }
if you want to save a boolean value so test on the
selectedId
of yourRadioButtons
and save a 0 or 1 to your database column (Example of two radio buttons to enable/disable updates) :// get selected radio button from radioGroup int selectedId = radioGroup.getCheckedRadioButtonId(); boolean isAllowUpdate = false; switch(selectedId) { case R.id.radioAllowUpdate : isAllowUpdate = true; break; case R.id.radioDisableUpdate : isAllowUpdate = false; break; } //save it to database if(isAllowUpdate) // true ==> save 1 value else // false ==> save 0 value
EDIT :
if you should control the selected value and when send it to database, see this tutorial
这篇关于在SQLite数据库存储单选按钮值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!