在 sqlite 数据库中存储单选按钮值

storing radio button value in sqlite database(在 sqlite 数据库中存储单选按钮值)
本文介绍了在 sqlite 数据库中存储单选按钮值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单选组,其中有两个单选按钮.我想获取单选按钮的值,然后将其存储在数据库中..我该怎么做?请帮忙!我搜索了它,但一切都是徒劳的!我尝试了这段代码,但我的活动在使用后停止工作

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

推荐答案

  1. 如果您想存储 RadioButton 的文本标签,请使用:

  1. 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();
}

  • 如果您想保存布尔值,请测试 RadioButtonsselectedId 并将 0 或 1 保存到数据库列(启用/禁用更新的两个单选按钮示例):

  • if you want to save a boolean value so test on the selectedId of your RadioButtons 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
    

  • 如果您应该控制所选值并将其发送到数据库,请参阅此 教程

    if you should control the selected value and when send it to database, see this tutorial

    这篇关于在 sqlite 数据库中存储单选按钮值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

    本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

    相关文档推荐

    How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
    Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
    How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
    How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
    Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
    Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)