CoOkie
CoOkie

Reputation: 84

setOnClickListeners for dynamic created Checkboxes

I'm facing a problem while trying to create Checkboxes Dynamic in my application. The design works fine, and I'm able to create as many Checkboxes as i want. The Checkboxes are put into a TableRow together with a TextView so that the text is at left side of the checkbox. But my problem is, that in my activity i can get the "status" of the checkbox, whether it's checked or not. I use the inflater to create my Checkboxes. The xml for the checkboxes:

<TableRow xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/TableRow" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:paddingLeft="20dip" android:id="@+id/tv_effect" android:gravity="left" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:textColor="#000" android:textSize="18dip" android:layout_width="wrap_content"></TextView>
<CheckBox android:id="@+id/cb_effect" android:layout_height="wrap_content" android:text="" android:layout_gravity="right" android:layout_width="wrap_content"></CheckBox>

The function i call to create a new tablerow containing a textview and a checkbox:

    public void layoutMakeSpeakerEffect(String effectName,int effectNumber)
{
    LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View myViewSp = linflater.inflate(R.layout.speaker_settings, null);

    final TextView tv_effect = (TextView) myViewSp.findViewById(R.id.tv_effect);
    tv_effect.setId(effectNumber);
    tv_effect.setText(effectName);

    final CheckBox cb_effect = (CheckBox) myViewSp.findViewById(R.id.cb_effect);
    cb_effect.setId(effectNumber);
    cb_effect.setText("");

    cb_effect.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) { 
              if(cb_effect.isChecked())
              {
                  Toast toast = Toast.makeText(getApplicationContext(), ""+tv_effect.getText()+": ON", Toast.LENGTH_SHORT);
                  toast.show();
              }
              else
              {
                  Toast toast = Toast.makeText(getApplicationContext(), ""+tv_effect.getText()+": OFF", Toast.LENGTH_SHORT);
                  toast.show(); 
              }
          }
    });

    tl_speakerSettings.addView(myViewSp);   
}

And as mentioned, the design works fine. But how am I able to outside this function to get the "status" off the checkbox? And i also need a function that could clear the checkboxes status, and another function that could enable and disable the checkboxes?

I can't seem to figure this problem out by myself.

My only idea, is to make something that checks the "cb_effects" ID and afterwards checks the status of the desired checkbox.

Upvotes: 3

Views: 3700

Answers (2)

Tushar Vengurlekar
Tushar Vengurlekar

Reputation: 7679

You can make use of a global integer array that will contain as many elements as there are rows in table. Set the value of the element to 1 if checkbox is checked and 0 for unchecked. By finding which element is having 1 you can fing which of the checkboxes are checked. Hope this works for you.

Upvotes: 1

Cristian
Cristian

Reputation: 200080

Use setOnCheckedChangeListener instead. It will fire the onCheckedChanged(CompoundButton buttonView, boolean isChecked) method, where you can now the state of the checkbox reading the isChecked variable.

Upvotes: 4

Related Questions