How to dynamically add buttons with a command argument for the OnClick Event
<p>I am coming across an issue with dynamically creating buttons.  I have my text I am adding to the buttons, however I also have a command argument I want to send with it.  I want my dynamically created buttons to open up a new <code>Activity</code> and pass this argument via <code>Intent</code>. I am a .NET guy and this would be easily done with a <code>CommandParameter</code> off of the <code>Button</code>.</p>

<p>My question is, is this the following code best way to accomplish this task?  If so, how can I pass command arguments to the click event.  If not, what should be my approach?</p>

<pre><code>int counter =0;

TableLayout layout = (TableLayout) findViewById(R.id.tableLayout);

while (counter&lt; list.size())
{
    MyObj obj = list.get(counter);

    Button b = new Button(this);
    b.setText(obj.getName());   
    // CommandParameter = obj.getId().toString();

    b.setOnClickListener(new OnClickListener(){
        public void onClick(View v)
        {
            Context ctx = getApplicationContext(); 

            Intent intent = new Intent(ctx, TestScreen.class);
            intent.putExtra("Id", "MyCommandParameter");
            startActivity(intent) ;
        }
    });

    layout.addView(b);
    counter++;
}
</code></pre>
