0

I am trying to send value to server using json,what I am trying is when I select checkbox I want to send "Y",,in my json active=Y is status of user activation and deactivation,,following is code what i tried,but its not working,it always shows active=N

 proactives=(CheckBox)findViewById(R.id.edt_proactive);
     if(proactives.isChecked()==true)
      {
          proactives.setTag("Y");
      }
      if(proactives.isChecked()==false)
      {
          proactives.setTag("N");
      }
fazilpuriasa
  • 297
  • 3
  • 8
  • 19

5 Answers5

2

try this

proactives.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if(b)
            {
                proactives.setTag("Y");
            }
            if(proactives.isChecked()==false)
            {
                proactives.setTag("N");
            }
        }
    });

OR

proactives.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
              proactives.setTag(b? "Y" : "N"); 
            }
        });
Nooh
  • 1,548
  • 13
  • 21
  • 3
    proactives.isChecked()==true is not necessary, since isChecked already returns a boolean, which you are comparing with true. Also, since boolean b represents the checked value, doing proactives.isChecked() is also not needed, you could replace all that code with if (b) {...}else{...} – 2Dee Mar 20 '15 at 10:41
  • can you help http://stackoverflow.com/questions/29183249/autocompletetextview-not-working – fazilpuriasa Mar 23 '15 at 04:13
1

you can use OnCheckedChangeListener

proactives.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // change your tag here
        }
    });
1

Use CompoundButton.OnCheckedChangeListener :

proactives.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

       @Override
       public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
           // use isChecked value here to react to state changes on your checkbox
       }
   }
);   
2Dee
  • 8,609
  • 7
  • 42
  • 53
1

give onclick in layout file

<CheckBox
    android:id="@+id/edt_proactive"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="itemClicked" />

in your activity do this

public void itemClicked(View v) {
    if (((CheckBox) v).isChecked()) {
        proactives.setTag("Y");
    }else{
        proactives.setTag("N");
}
2Dee
  • 8,609
  • 7
  • 42
  • 53
Md Hussain
  • 411
  • 4
  • 10
0

faster way :P

proactives=(CheckBox)findViewById(R.id.edt_proactive);
proactives.setTag(proactives.isChecked() ? "Y" : "N");

edit: and use OnCheckedChangeListener as above answers mention

snachmsm
  • 17,866
  • 3
  • 32
  • 74