0
我想使用复选框将数据插入数据库,但我不知道如何去做。如果用户选择一个复选框并单击更新按钮,它将被插入到数据库中。如何使用复选框在数据库中插入数据?
这是我的代码。
CXATest1Activity
public class CXATest1Activity extends Activity {
/** Called when the activity is first created. */
DBCXAAdapter db = new DBCXAAdapter(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set up main content view
setContentView(R.layout.main);
Button button1main = (Button) findViewById(R.id.Button01main);
button1main.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
db.open();
EditText editTextName = (EditText)findViewById(R.id.EditTextName);
String NameValue = editTextName.getText().toString();
EditText editTextAge = (EditText)findViewById(R.id.EditTextAge);
String AgeValue = editTextAge.getText().toString();
EditText editTextMedicalBackgroundBody = (EditText)findViewById(R.id.EditTextMedicalBackgroundBody);
String MedicalBackgroundBodyValue = editTextMedicalBackgroundBody.getText().toString();
Spinner editTextSpinnerGenderType = (Spinner)findViewById(R.id.SpinnerGenderType);
String GenderTypeValue = editTextSpinnerGenderType.getSelectedItem().toString();
Spinner editTextSpinnerMedicalBackgroundSelectType = (Spinner)findViewById(R.id.SpinnerMedicalBackgroundSelectType);
String MedicalBackgroundSelectTypeValue = editTextSpinnerMedicalBackgroundSelectType.getSelectedItem().toString();
CheckBox editTextCheckboxMedicalBackgroundCancer = (CheckBox)findViewById(R.id.checkBoxCancer);
String MedicalBackgroundCancerValue = editTextCheckboxMedicalBackgroundCancer.getText().toString();
editTextCheckboxMedicalBackgroundCancer.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
long id;
id = db.insertUserprofile(NameValue, AgeValue, MedicalBackgroundBodyValue, GenderTypeValue, MedicalBackgroundSelectTypeValue, MedicalBackgroundCancerValue);
db.close();
//set up dialog
final Dialog dialog = new Dialog(CXATest1Activity.this);
dialog.setContentView(R.layout.maindialog);
dialog.setTitle("Congrats");
dialog.setCancelable(true);
//there are a lot of settings, for dialog, check them all out!
//set up text
TextView text = (TextView) dialog.findViewById(R.id.TextView01);
text.setText(R.string.lots_of_text);
//set up image view
ImageView img = (ImageView) dialog.findViewById(R.id.ImageView01);
img.setImageResource(R.drawable.updated_icon_new1);
//set up button
Button button = (Button) dialog.findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
//now that the dialog is set up, it's time to show it
dialog.show();
}
DBCXAAdapter
//---insert a userprofile into the database---
public long insertUserprofile(String name, String age, String medicalbackground, String spinnergender, String spinnermedicalbackground, String checkboxmedicalbackgroundcancer)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_AGE, age);
initialValues.put(KEY_MEDICALBACKGROUND, medicalbackground);
initialValues.put(KEY_SPINNERGENDER, spinnergender);
initialValues.put(KEY_SPINNERMEDICALBACKGROUND, spinnermedicalbackground);
initialValues.put(KEY_CHECKBOXMEDICALBACKGROUNDCANCER, checkboxmedicalbackgroundcancer);
return db.insert(DATABASE_TABLE, null, initialValues);
}
预先感谢。
我已经厌倦了使用你的建议,但它没有奏效。 Wheather我检查了是否复选框,它仍然将“Cancer”值插入到数据库中。 有没有我应该写的其他代码? – 2012-03-28 07:40:49
我已经按照你所说的完成了,但是有错误“不能用不同的方法定义的内部类中的非最终可变的AgeValue”。另外,我认为public(void)会导致错误。我编辑了CXATest1Activity.java类来添加我之前没有添加的公共void menthod。 – 2012-03-28 10:05:23
如果用户没有点击复选框,那么复选框不会将数据插入到数据库中。我怎么做? – 2012-03-29 05:39:29