0

I am dynamically generating checkboxes in my program like so:

public void addNewItem(String item, TableLayout tablel) {
    TableRow row = new TableRow(this);
    TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
    row.setLayoutParams(params);
    CheckBox item1 = new CheckBox(this);
    item1.setText(item);
    row.addView(item1);
    tablel.addView(row, i);
    i++;

From what I've been able to test, this work fine for adding checkboxes to my table. The problem I'm encountering is that I want to be able to have something happen when a checkbox is checked, which I am unsure of how to do without knowning the id. Is there some way to get around this or get the id of the checkbox that has been checked when the onCheckBoxClick() method is called?

AndyReifman
  • 1,459
  • 4
  • 18
  • 34

3 Answers3

0

You don't need to know the ID because you already have the checkbox as on object.

Use this:

item1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(item1.isChecked()){
                System.out.println("Checked");
            }else{
                System.out.println("Un-Checked");
            }
        }
    });

An other possibility:

item1.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        if ( isChecked )
        {
            // perform logic
        }

    }
});
Robin Dijkhof
  • 18,665
  • 11
  • 65
  • 116
  • 1
    You may also get the id from inside the onclick listener by calling the getId method on the view, in this case it would be `v.getId()`. See the checkbox guide [here](http://developer.android.com/guide/topics/ui/controls/checkbox.html) as it also talks about aanother method for setting the onClick method. – Spaceman Spiff Nov 06 '14 at 18:02
  • And you can also generate an id by youself by calling `v.setId(View.generateViewId())`; – Pedro Oliveira Nov 06 '14 at 18:04
  • I think the problem with this is that I plan on generating several checkboxes, each in their own table row. Then, if they get checked, I remove them. Therefore, I'm not sure this is totally what I want to use. – AndyReifman Nov 06 '14 at 18:17
  • I do not see what the problem is with that – Robin Dijkhof Nov 06 '14 at 18:46
0

Assign id via code (programmatically)

1.Manually set ids using someView.setId(int);

2.The int must be positive, but is otherwise arbitrary. Then you can access that id.

It think this would help you.

thestrongenough
  • 225
  • 2
  • 14
0

You have created TableRow and CheckBox you should set id pragmatically like this

public void addNewItem(String item, TableLayout tablel) {
TableRow row = new TableRow(this);
row.setId(i);//i is a positive int value
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(params);
CheckBox item1 = new CheckBox(this);
row.setId(j);//j is a positive int value
item1.setText(item);
row.addView(item1);
tablel.addView(row, i);
i++;

You can check this SO question

int chkBoxId = 10;
int tableRowId = 100;
String texts[] = {"Text1", "Text2", "Text3", "Text4", "Text5"};
CheckBox[] chkBoxes;
private TableLayout tableLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dynamic_chk_box);
    tableLayout = (TableLayout) findViewById(R.id.tableLayout);
    chkBoxes = new CheckBox[texts.length];
    for(int i = 0; i < 5; i++) {
        TableRow row = new TableRow(this);
        TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
        row.setLayoutParams(params);
        chkBoxes[i] = new CheckBox(this);
        chkBoxes[i].setId(chkBoxId++);
        row.setId(tableRowId);//tableRowId is a positive int value
        chkBoxes[i].setText(texts[i]);
        row.addView(chkBoxes[i]);
        tableLayout.addView(row, i);
        tableRowId++;
    }
    for (int i = 0; i < texts.length; i++) {
        final int j = i;
        chkBoxes[j].setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub

            if(isChecked) {
                int checkedId = chkBoxes[j].getId();
                Toast.makeText(DynamicCheckBoxActivity.this, 
                        String.valueOf(checkedId), 
                        Toast.LENGTH_SHORT).show();
            } else {
                int unCheckedId = chkBoxes[j].getId();
                System.out.println("Uncheck ===> " + String.valueOf(unCheckedId));
            }
          }
      });
    }
}
Community
  • 1
  • 1
Kaushik
  • 6,150
  • 5
  • 39
  • 54
  • This may be a stupid question, but if I then wanted to check which has been checked, is there any easy way to recall that id once I set it? – AndyReifman Nov 06 '14 at 18:31