3

我有这个数据库查询:

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


这是我的复选框代码:

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


但这不起作用......
我想在选中或未选中时将值存储到数据库中
我怎么能这样做?

谢谢你。

4

2 回答 2

1

第一个问题:

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

第二期:

在单击按钮时,您正在尝试调用checkBox数据库的方法,在这里您再次获取 CheckBox 标题的值,因此在此处选中或取消选中复选框都没有关系。

于 2012-04-17T10:50:10.810 回答
1

如果选中了一个复选框,您可以获得布尔值(真或假)value = checkbox.isChecked();

如果为真,则在您的数据库中另存为 1,如果不是,则另存为 0

于 2012-04-17T10:21:51.233 回答