2014-09-20 63 views
0

我將製作一個收集用戶個人信息的簡單應用程序。但現在我有一些單選按鈕的問題。我想將選定的單選按鈕值添加到SQL。 這是代碼。當點擊所選的單選按鈕時,出現錯誤

MainActivity:

public void add(View view){ 
    EditText fn = (EditText)findViewById(R.id.first_name); 
    EditText ln = (EditText)findViewById(R.id.last_name); 

    first_name=fn.getText().toString(); 
    last_name =ln.getText().toString(); 

    radio_group=(RadioGroup)findViewById(R.id.radio_gender); 
    String gender= ((RadioButton)this.findViewById(radio_group.getCheckedRadioButtonId())).getText().toString(); 

    pf.execSQL("INSERT INTO Persone VALUES('"+first_name+"','"+last_name+"','"+gender+"');"); 
} 
+0

默認情況下是否有任何按鈕被選中?你應該在RadioButton上添加一個onCkeckedChangeListener – joao2fast4u 2014-09-20 19:29:58

回答

0

你能以某種方式比較開關情況下,單選按鈕的方法。

這裏是你的代碼怎麼會看起來像一個例子:

public void onRadioButtonClicked(View view) { 
    // Is the button now checked? 
boolean checked = ((RadioButton) view).isChecked(); 

// Check which radio button was clicked 
switch(view.getId()) { 
    case R.id.radio_pirates: 
     if (checked) 
      // Pirates are the best 
     break; 
    case R.id.radio_ninjas: 
     if (checked) 
      // Ninjas rule 
     break; 
} 
} 

所以,實際上你需要指定當選擇從無線電集團特定的單選按鈕會發生什麼。

如果你想要一個更詳細的//到點答案讓我知道在一個評論,我會嘗試寫你需要使用您的主要活動的代碼。

相關問題