将CheckBox值插入SQLite Android

时间:2012-04-17 10:15:25

标签: android database sqlite checkbox

我有这个数据库查询:

public void checkBox(String fasil1,String fasil2,String fasil3,String fasil4,String fasil5,String fasil6,String fasil7,
        String fasil8,String fasil9,String fasil10,String fasil11,String fasil12,String fasil13,String fasil14){
    ContentValues cv = new ContentValues();
    cv.put("fasil1", fasil1);
    cv.put("fasil2", fasil2);
    cv.put("fasil3", fasil3);
    cv.put("fasil4", fasil4);
    cv.put("fasil5", fasil5);
    cv.put("fasil6", fasil6);
    cv.put("fasil7", fasil7);
    cv.put("fasil8", fasil8);
    cv.put("fasil9", fasil9);
    cv.put("fasil10", fasil10);
    cv.put("fasil11", fasil11);
    cv.put("fasil12", fasil12);
    cv.put("fasil13", fasil13);
    cv.put("fasil14", fasil14);

    getWritableDatabase().insert("fasil", "name", cv);
}


这是我的CheckBox代码:

fasil1 = (CheckBox)findViewById(R.id.rs);
    fasil1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        //@Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
             if(isChecked){
                  checked = "Rumah Sakit";
                  Toast.makeText(CobaCombo.this, checked, Toast.LENGTH_SHORT).show();
             }else{
                  checked = "-";

             }
        }
   });
    fasil2 = (CheckBox)findViewById(R.id.skul);
    fasil2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        //@Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
             if(isChecked){
                  checked = "Sekolah";
                  Toast.makeText(CobaCombo.this, checked, Toast.LENGTH_SHORT).show();
             }else{
                  checked = "-";

             }
        }
   });
    fasil3 = (CheckBox)findViewById(R.id.kampus);
    fasil3.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        //@Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
             if(isChecked){
                  checked = "Kampus";
                  Toast.makeText(CobaCombo.this, checked, Toast.LENGTH_SHORT).show();
             }else{
                  checked = "-";
             }
        }
   });
    fasil4 = (CheckBox)findViewById(R.id.officeBuild);
    fasil4.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        //@Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
             if(isChecked){
                  checked = "Office Building";
                  Toast.makeText(CobaCombo.this, checked, Toast.LENGTH_SHORT).show();
             }else{
                  checked = "-";
             }
        }
   });
    fasil5 = (CheckBox)findViewById(R.id.apartemen);
    fasil5.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        //@Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
             if(isChecked){
                  checked = "Apartemen";
                  Toast.makeText(CobaCombo.this, checked, Toast.LENGTH_SHORT).show();
             }else{
                  checked = "-";
             }
        }
   });
    fasil6 = (CheckBox)findViewById(R.id.hotel);
    fasil6.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        //@Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
             if(isChecked){
                  checked = "Hotel";
                  Toast.makeText(CobaCombo.this, checked, Toast.LENGTH_SHORT).show();
             }else{
                  checked = "-";
             }
        }
   });
    fasil7 = (CheckBox)findViewById(R.id.bank);
    fasil7.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        //@Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
             if(isChecked){
                  checked = "Bank";
                  Toast.makeText(CobaCombo.this, checked, Toast.LENGTH_SHORT).show();
             }else{
                  checked = "-";
             }
        }
   });
    fasil8 = (CheckBox)findViewById(R.id.spbu);
    fasil8.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        //@Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
             if(isChecked){
                  checked = "SPBU";
                  Toast.makeText(CobaCombo.this, checked, Toast.LENGTH_SHORT).show();
             }else{
                  checked = "-";
             }
        }
   });
    fasil9 = (CheckBox)findViewById(R.id.pasar);
    fasil9.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        //@Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
             if(isChecked){
                  checked = "Pasar";
                  Toast.makeText(CobaCombo.this, checked, Toast.LENGTH_SHORT).show();
             }else{
                  checked = "-";
             }
        }
   });
    fasil10 = (CheckBox)findViewById(R.id.wisata);
    fasil10.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        //@Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
             if(isChecked){
                  checked = "Jalur Wisata / Tempat Wisata";
                  Toast.makeText(CobaCombo.this, checked, Toast.LENGTH_SHORT).show();
             }else{
                  checked = "-";
             }
        }
   });
    fasil11 = (CheckBox)findViewById(R.id.terminal);
    fasil11.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        //@Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
             if(isChecked){
                  checked = "Stasiun / Terminal";
                  Toast.makeText(CobaCombo.this, checked, Toast.LENGTH_SHORT).show();
             }else{
                  checked = "-";
             }
        }
   });
    fasil12 = (CheckBox)findViewById(R.id.bandara);
    fasil12.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        //@Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
             if(isChecked){
                  checked = "Bandara / Airport";
                  Toast.makeText(CobaCombo.this, checked, Toast.LENGTH_SHORT).show();
             }else{
                  checked = "-";
             }
        }
   });
    fasil13 = (CheckBox)findViewById(R.id.mol);
    fasil13.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        //@Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
             if(isChecked){
                  checked = "Mal / Shoping Center";
                  Toast.makeText(CobaCombo.this, checked, Toast.LENGTH_SHORT).show();
             }else{
                  checked = "-";
             }
        }
   });
    fasil14 = (CheckBox)findViewById(R.id.hiburan);
    fasil14.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        //@Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
             if(isChecked){
                  checked = "Entertainment";
                  Toast.makeText(CobaCombo.this, checked, Toast.LENGTH_SHORT).show();
             }else{
                  checked = "-";
             }
        }
   });

然后我有代码在单击按钮时保存值:

helper.checkBox(fasil1.getText().toString(), fasil2.getText().toString(), fasil3.getText().toString(), 
                    fasil4.getText().toString(), fasil5.getText().toString(), fasil6.getText().toString(), 
                    fasil7.getText().toString(), fasil8.getText().toString(), fasil9.getText().toString(),
                    fasil10.getText().toString(), fasil11.getText().toString(), fasil12.getText().toString(), 
                    fasil13.getText().toString(), fasil14.getText().toString());


但这不起作用...... 我想在选中或取消选中时将值 已检查 存储到数据库中。
我该怎么做?

谢谢。

2 个答案:

答案 0 :(得分:1)

如果选中复选框,则可以使用value = checkbox.isChecked();

获取bool值(true或false)

如果为真,则在数据库中保存为1,如果不保存为0

答案 1 :(得分:1)

第一期:

您在每个选中的复选框上为checked字符串变量赋值,因此最后您将只有1个字符串值,无论您是选择了2个复选框还是单个,而是应该创建String []数组选中复选框。

第二期:

在按钮上单击,您试图调用数据库的checkBox方法,此处您只是获取CheckBox标题的值,因此无论是选中还是取消选中复选框都无关紧要。< / p>