0

i have created a list of checkbox dynamically in my app..i want to detect which check box is clicked ...please help here is my code..

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_place_bid);
  String[] sh=item_list();
  int array_length=jArray.length();
  LinearLayout linearLayout = (LinearLayout) findViewById(R.id.lyout);

  for(int c=0; c<jArray.length();c++){
    CheckBox chk=new CheckBox(this);
    chk.setId(c+1);
    chk.setText("Click to add values");
    chk.setTextColor(Color.GRAY);
    linearLayout.addView(chk);
  }

  for(a = 1;a<=array_length;a++){
    CheckBox  satView = (CheckBox)findViewById(a);  
    satView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
        String s="x"+a;
        Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();   
      }
    });
  }
}
8
  • You are not creating check box dynamically.. You are recreating single check box in for loop. It may work if you change it to CheckBox satView = new CheckBox(); Commented Jan 2, 2014 at 9:13
  • so what is happening this code not working, error, exception?? Commented Jan 2, 2014 at 9:15
  • NO, Its new Checkbox(), its not same checkbox :) Looks like never trierd to add a click listener Commented Jan 2, 2014 at 9:16
  • i think this is listener code... satView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) { String s="x"+a; Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show(); } }); Commented Jan 2, 2014 at 9:22
  • @saa Its not working ...i cant understand how to use CheckBox satView = new CheckBox(); it is getting me error "The constructor CheckBox() is undefined" Commented Jan 2, 2014 at 9:23

1 Answer 1

3

Try this ..

  @Override
        protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_place_bid);
          String[] sh=item_list();
          int array_length=jArray.length();
          LinearLayout linearLayout = (LinearLayout) findViewById(R.id.lyout);

          for(int c=0; c<jArray.length();c++){
            CheckBox chk=new CheckBox(this);
            chk.setId(c+1);
            chk.setText("Click to add values");
            chk.setTextColor(Color.GRAY);
            chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
              @Override
              public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
               String s="x"+buttonView.getId();
               Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();

                switch(buttonView.getId()){

                  case 1: // do something on 1st checkbox
                        break;

                  case 2: //do something on 2nd  checkbox
                       break;

                //And SO ON for all checkboes
            }  
           }
          });
            linearLayout.addView(chk);
          }


        }

If SetId(Int) is not working then you can use setTag(int) and getTag() instead.

Sign up to request clarification or add additional context in comments.

3 Comments

its only working for 1st checkbox..rest of the checkbox are not functioning
You wrote code for all switch cases there ? check my edit now, should give out some Toast msg for each checkboes .
Awesome this is what I am searching for Hours...!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.