如何从编程单选按钮/复选框中获取值?

时间:2014-06-07 16:27:18

标签: android

我有一个表单,问题来自Web服务(可能有复选框或文本字段或单选按钮编程)并将值发送到本地数据库。 第一部分(调用Web服务并显示它们)是正确的,但第二部分(获取值)不是! 如何从programmatic-ally单选按钮/复选框中获取值? (重要的部分是我有两个" FOR"用于单选按钮或复选框)

我不知道怎样才能获得价值!??!??! 这是我的代码,提出了问题! PLZ帮助我。

for (int i = 0; i < output2.length; i++) {
if (output2[i].contains("#")) {
part1 = output2[i].split("#");
int i2 = 0;
do {

// Question Id ->part1[0]
// FK_MASTERNAZAR ->partinfo_id[0]
// Tilte ->part1[1]

TextView tv2 = new TextView(page2.this);
tv2.setId(i2);
tv2.setText(part1[1]);
tv2.setGravity(Gravity.RIGHT);
lmain2.addView(tv2);

// Answers ->part1[2]
// Type ->part1[3]

switch (Integer.valueOf(part1[3])) {

case 1:
// single selection
part2 = part1[2].split(",");
radioGroup1 = new RadioGroup(page2.this);
radioGroup1.setGravity(Gravity.RIGHT);
radioGroup1.setOrientation(RadioGroup.HORIZONTAL);

for (int i3 = 0; i3 < part2.length; i3++) {
LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
rb6 = new RadioButton(page2.this);
rb6.setId(i3);
rb6.setText(part2[i3]);
rb6.setGravity(Gravity.RIGHT);
radioGroup1.addView(rb6, 0, layoutParams);
}
lmain2.addView(radioGroup1);
break;

case 2:
et_Num = new EditText(page2.this);
et_Num.setId(i2);
et_Num.setHint("insert your answer…");
et_Num.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED);
lmain2.addView(et_Num);
break;

case 3:
// multi selection
part2 = part1[2].split(",");
LinearLayout lcheck = new LinearLayout(page2.this);
lcheck.setGravity(Gravity.RIGHT);
lcheck.setOrientation(LinearLayout.HORIZONTAL);
for (int i3 = 0; i3 < part2.length; i3++) {

ch6 = new CheckBox(page2.this);
ch6.setId(i3);
ch6.setText(part2[i3]);
ch6.setGravity(Gravity.RIGHT);
lcheck.addView(ch6);
lmain2.addView(lcheck);
break;

}

// T ->part2[4]
// insert line
TextView tv6 = new TextView(page2.this);
tv6.setId(i2);
tv6.setText("**********");
tv6.setGravity(Gravity.CENTER);
tv6.setTextColor(Color.parseColor("#33B5E5"));
lmain2.addView(tv6);

Question_Id = Integer.valueOf(part1[0]);
Integer qtype = Integer.valueOf(part1[4]);

// copy FormQuestion web service to the database
database = new MySQLiteHelper(getApplicationContext());
FormQuestionClass formquestionclass = new FormQuestionClass(
Question_Id, Fk_masternazar, part1[2], part1[3], qtype);
database.InsertQuestion(formquestionclass);
database.close();
// end insertting to db.

} while (i2 == part1.length);
}
}

1 个答案:

答案 0 :(得分:0)

将添加的控件用作字段而不是局部变量。

这样你可以稍后检查null,如果它们不为null,请使用

RadioGroup.getCheckedRadioButtonId()
EditText.getText()