将值从单选按钮传递到数据库

时间:2013-08-17 10:48:19

标签: android radio-button radiobuttonlist

我做了一个包含单选按钮的布局。我想将所选单选按钮的值传递给数据库,以便如何获取此值。   代码如下。请帮助我..

private View drawRadioTypeQuestion(Question question, ArrayList<QuestionOption> questionOptions) {
        LinearLayout layout = new LinearLayout(this);
        layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setPadding(5, 5, 5, 5);

        TextView tv = new TextView(this);
        tv.setText(question.Question_Order + ". " + question.Question_Title);
        tv.setTextSize(16);
        // tv.setTypeface(PhoneStatus.getRobotoMediumTypeface(this));
        layout.addView(tv);

        RadioGroup rg = new RadioGroup(this);
        rg.setOrientation(RadioGroup.VERTICAL);
        for (QuestionOption opt : questionOptions) {
            RadioButton rb = new RadioButton(this);
            rb.setText(opt.Question_Answer_Value);
            rb.setTextSize(16);
            rg.addView(rb);
        }
        layout.addView(rg);

        PhoneStatus.overrideFonts(QuestionListActivity.this, layout);
        tv.setTypeface(PhoneStatus.getRobotoMediumTypeface(this));
        return layout;
    }

2 个答案:

答案 0 :(得分:0)

在单选按钮上实现点击侦听器。在onclick事件中,将所选单选按钮的值添加到数据库..

访问此处获取官方文件

http://developer.android.com/guide/topics/ui/controls/radiobutton.html

http://android-er.blogspot.in/2009/11/radiogroup-and-radiobutton.html

答案 1 :(得分:0)

试试这段代码

   rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // TODO Auto-generated method stub
            RadioButton rb = (RadioButton) findViewById(checkedId);
            String value = rb.getText().toString();
        }
    });