如何使用复选框在数据库中插入数据?

时间:2012-03-28 06:36:49

标签: android sqlite checkbox

我正在尝试使用复选框将数据插入数据库,但我不知道如何去做。如果用户选中一个复选框并单击更新按钮,它将被插入到数据库中

这是我的代码。

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

提前感谢。

1 个答案:

答案 0 :(得分:0)

        id = db.insertUserprofile(NameValue, AgeValue, MedicalBackgroundBodyValue, GenderTypeValue, MedicalBackgroundSelectTypeValue, MedicalBackgroundCancerValue); all you have to do is add listener to your checkbox which listens of checkbox activity



editTextCheckboxMedicalBackgroundCancer.setOnCheckedChangeListener(new OnCheckedChangeListener(){
         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // TODO Auto-generated method stub
         }
    });

<强>更新

嗯,你没有在复选框监听器内做,你在监听器之外调用insertUserprofile函数,它将始终执行。

editTextCheckboxMedicalBackgroundCancer.setOnCheckedChangeListener(new OnCheckedChangeListener(){
         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
long id;
id = db.insertUserprofile(NameValue, AgeValue, MedicalBackgroundBodyValue, GenderTypeValue, MedicalBackgroundSelectTypeValue, MedicalBackgroundCancerValue); 

         }
    });